This is my code.

$arrivDate = $_POST['arrival_date'];
echo $arrivDate; //Print correctly

echo date("Y/m/d", strtotime($arrivDate)) ; // every time print 1969/12/31
echo date("Y/m/d", strtotime('.$arrivDate.')) ; // every time print 1969/12/31

but if i put like this, it is print correctly

echo date("Y/m/d",  strtotime("05/08/2019")); // Print like this --> 2019/08/05 (This is correct)

My question is if i put hard coded value, it is print very well. but i cant put a variable to it. evry time print a garbage value. im sure that $_POST['arrival_date'] part has no any problem because according to my 2nd line, the correct value is shown.
Please tell me the reason for this....

Recommended Answers

You don't show the format of the $_POST['arrival_date'].

You're better to use the DateTime object - create from a specified format and output to a new format. Easy.

$date = DateTime::createFromFormat('d/m/Y', $_POST['arrival_date']);
echo $date->format('Y/m/d');
Jump to Post

You need the php 5.3 verion for the DateTime::createFromFormat, it was introduced in that version.

Jump to Post

All 6 Replies

Member Avatar

You don't show the format of the $_POST['arrival_date'].

You're better to use the DateTime object - create from a specified format and output to a new format. Easy.

$date = DateTime::createFromFormat('d/m/Y', $_POST['arrival_date']);
echo $date->format('Y/m/d');

My format is 'd/m/Y'.
the code is not working....
showing this error
Call to undefined method DateTime::createFromFormat() in //path
I think im using a lower version of PHP. Please tell me some other method to do this....

You need the php 5.3 verion for the DateTime::createFromFormat, it was introduced in that version.

Member Avatar

My format is 'd/m/Y'.

From the manual -

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.

You're out of luck with strtotime.

You can easily replace the '/' with '-' though with str_replace.

This is what i did. Searching by searching i found this. This method can change the format of a date. My problem is solved. Thank you very much everyone for replying. I post my code because of some other begginer also can get idea from this....

        $arrivDate = $_POST['arrival_date'];
        list($day, $month, $year) = explode('/', $arrivDate);
        $timestamp = mktime(0, 0, 0, $month, $day, $year);
        $arrivDate =  date("Y-m-d", $timestamp);
Member Avatar

You could have done this, as I suggested:

$arrivDate = str_replace("/","-",$_POST['arrival_date']);
$arrivDate = date("Y-m-d", strtotime($arrivDate));

Seems less hassle than creating an array and then a timestamp.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.