PHP Date Conversion to Strtotime

PHP Convert date using strtotime?

You want DateTime::createFromFormat() (http://php.net/manual/en/datetime.createfromformat.php). Using that you can convert a string to a datetime any way you want to.

PHP date conversion to strtotime

You need to understand over here the date format of your date.
Here m/d/Y is considered to be the standard American Date format.

So when your date is like

'09/06/2014',//it should be considered as 06 september 2014
'04/07/2014',//it should be considered as 07 april 2014

And when your date is like as

'21/05/2014',
'22/05/2014',

Then the above date doesn't fits within the standards of American Date Format i.e. m/d/Y instead what you can do over here is replace / along with - which converts your date into European Date Format which is d-m-Y

PHP Converting Integer to Date, reverse of strtotime

Yes you can convert it back. You can try:

date("Y-m-d H:i:s", 1388516401);

The logic behind this conversion from date to an integer is explained in strtotime in PHP:

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

For example, strtotime("1970-01-01 00:00:00") gives you 0 and strtotime("1970-01-01 00:00:01") gives you 1.

This means that if you are printing strtotime("2014-01-01 00:00:01") which will give you output 1388516401, so the date 2014-01-01 00:00:01 is 1,388,516,401 seconds after January 1 1970 00:00:00 UTC.

Converting Date format in PHP

Use strtotime() and date():

$originalDate = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)" ;
$newDate = date("Y-m-d H:i:s", strtotime($originalDate));

(see strtotime and date docs on the PHP site).

or use DateTime:

<?php
$source = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)";
$date = new DateTime($source);
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
?>

DEMO

PHP date format and strtotime

The solution is remove special characters from the date string:

date("Y-m-d", strtotime(preg_replace('/[^A-Za-z0-9\-]/', '', $dateString)))

Convert a date format in PHP

Use strtotime() and date():

$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));

(See the strtotime and date documentation on the PHP site.)

Note that this was a quick solution to the original question. For more extensive conversions, you should really be using the DateTime class to parse and format :-)

php date issue when converting using strtotime, but how to do this properly?

Yes, use DateTime object for this:

$test_date = '05-10-2016';
$DateTime = DateTime::createFromFormat('m-d-Y', $test_date, new DateTimeZone('utc'));
var_dump($DateTime);

OUTPUT

object(DateTime)[8]
public 'date' => string '2016-05-10 15:08:53.000000' (length=26)
public 'timezone_type' => int 2
public 'timezone' => string 'UTC' (length=3)

So

echo $DateTime->format('Y-m-d'); //2016-05-10 

How does PHP determine date format for a strtotime() call

strtotime() returns timestamp and it only parses date in specific format like "m/d/y" or "d-m-y" or "d.m.y". It is clearly state on documentation that:

Each parameter of this function uses the default time zone unless a
time zone is specified in that parameter.

And regarding format accepted:

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.

Hope it helps...



Related Topics



Leave a reply



Submit