Page 1 of 1

format date

Posted: Wed Dec 17, 2008 1:30 am
by kofmaster
i have been trying to convert a posted parameter from a textbox like '12/09/2008' m/d/Y to 'Y/d/m' but seem to be deleting the date when im trying to pass a variable instead of a string : see


$startDate = $_POST['arrival'];
$endDate = $_POST['departure'];


$ArrivalDate = new DateTime("@$startDate");
$DepartureDate = date("Y-d-m",strtotime("@$endDate"));

echo $ArrivalDate->format("Y/m/d");
echo $DepartureDate."<br>";

but both give me the same result. this 1970-01-01. Why?

the php version has been updated to the version 5.2.6

regards.

Re: format date

Posted: Wed Dec 17, 2008 1:50 am
by requinix
So I try your code with "12/09/2008" for both dates and I get

Code: Select all

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (@12/09/2008) at position 3 (/): Unexpected character' in X:Y
Stack trace:
#0 X(Y): DateTime->__construct('@12/09/2008')
#1 {main}
  thrown in X on line Y
Then I fix it (by removing the @) and I get

Code: Select all

2008/12/092008-09-12<br>
As far as I can tell it's working fine.

Re: format date

Posted: Wed Dec 17, 2008 1:54 am
by JAB Creations

Code: Select all

function user_date_mysql($date, $format) {return date ($format, strtotime($date));}
//function call example...
echo user_date_mysql($row1['thread_date'], "l F jS, Y, h:iA");
For date format specifics see the following PHP manual page...
http://php.net/date