How to parse a date string in PHP?
Try strtotime() to convert to a timestamp and then date() to get it in your own format.
Converting string to Date and DateTime
Use strtotime()
on your first date then date('Y-m-d')
to convert it back:
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2003-10-16
Make note that there is a difference between using forward slash /
and hyphen -
in the strtotime()
function. To quote from php.net:
Dates in the m/d/y or d-m-y formats
are disambiguated by looking at the
separator between the various
components: if the separator is a
slash (/), then the American m/d/y is
assumed; whereas if the separator is a
dash (-) or a dot (.), then the
European d-m-y format is assumed.To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
Convert DateTime to String PHP
You can use the format
method of the DateTime
class:
$date = new DateTime('2000-01-01');
$result = $date->format('Y-m-d H:i:s');
If format
fails for some reason, it will return FALSE
. In some applications, it might make sense to handle the failing case:
if ($result) {
echo $result;
} else { // format failed
echo "Unknown Time";
}
PHP parse string to dateTime() with
$datetime = DateTime::createFromFormat('Y-m-d\TH:i:s+', '2017-03-03T09:06:41.187');
print_r($datetime);
prints:
DateTime Object
(
[date] => 2017-03-03 09:06:41.000000
[timezone_type] => 3
[timezone] => Europe/Helsinki
)
How to parse date string to dateTime in php?
You could use the createFromFormat method from the DateTime object :
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s,u', "2014-12-01 16:00:02,153");
echo $dateTime->format('Y-m-d H:i:s');
PHP Parse Date From String
You can do that using the DateTime object
.
$Date = new DateTime('2013-07-09T04:58:23.075Z'); //create dateTime object
$Date->setTimezone(new DateTimeZone('Europe/Amsterdam')); //set the timezone
echo $Date->format('d/m/Y H:i');
PHP Parse Date String
explode
will do the trick for that:
$pieces = explode("/", $date);
$d = $pieces[1];
$m = $pieces[0];
$y = $pieces[2];
Alternatively, you could do it in one line (see comments - thanks Lucky):
list($m, $d, $y) = explode("/", $date);
PHP DateTime, parsing string date fails
Your input datetime string Wed Jan 12 2011 00:00:00 GMT 0100 (CET)
is not valid/standard for use in DateTime()
or strtotime()
. See date_parse()
function to see how your datetime string is being parsed:
print_r( date_parse('Wed Jan 12 2011 00:00:00 GMT 0100 (CET)') );
demo
Use DateTime::createFromFormat()
static method to return DateTime object according to the specific format.
demo
Related Topics
Checking If Your Code Is Running on 64-Bit PHP
PHP Daylight Saving Time Detection
Ajax Post Request in Laravel 5 Return Error 500 (Internal Server Error)
Getting Data with Utf-8 Charset from Mssql Server Using PHP Freetds Extension
What Does This Mean? "Parse Error: Syntax Error, Unexpected T_Paamayim_Nekudotayim"
Detect Clients with Proxy Servers via PHP
Get Most Recent Date from an Array of Dates in "Y-M-D H:I:S" Format
Why and How to Use Anonymous Functions in PHP
The Csrf Token Is Invalid. Please Try to Resubmit the Form
Php: Static and Non Static Functions and Objects
Finding the PHP File (At Run Time) Where a Class Was Defined
Many Hash Iterations: Append Salt Every Time