format date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kofmaster
Forum Newbie
Posts: 1
Joined: Wed Dec 17, 2008 1:24 am

format date

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: format date

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: format date

Post 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
Post Reply