Log in

View Full Version : how to change date format ?



sabahan
09-16-2009, 02:13 AM
i have date stored as type date: y-m-d (eg 2003-05-03)

i want it to be display as

3 May 2003

i tried this


$dateStart= date("j M y", strtotime($row['dateStart']));


but the result displayed 1 Jan 70

help

djr33
09-16-2009, 04:17 AM
It's best to store dates as the number of seconds since 1970, which is the standard format. It's something like 11000000 now, though I forget the number of zeros.
That is what date() is setup to deal with, so you can use that to output how you want and strtotime to make that.
I'm not sure about why it's not working, but there are probably ways to set strtotime to understand y-m-d.
But again, it's best to just always start with the default date() input (as just number of seconds since 1970) and it'll always work.

Hope this helps.

(Specifically about your code-- the output is correct, but the input is not. strtotime is not giving a reasonable value, so date() is assuming 0 seconds since 1970-- so you get 1 Jan 1970.)

Schmoopy
09-16-2009, 09:28 AM
Your code looks like it should work, maybe check how you've stored it in the database, this definitely works:



$datetime = '2003-05-03';

echo date('j M y', strtotime($datetime));

// Outputs 3 May 03

sabahan
09-16-2009, 11:32 AM
thanks..the date in db is correct and is stored as 2003-05-03

i use it like this and now it works




echo "" . date('j M Y', strtotime($row['dateStart'])) . "";



but i wonder why when i used it like this..the date is displayed as 1 Jan 1970



$mydate= date('j M Y', strtotime($row['dateStart']));

echo $mydate;

sabahan
09-16-2009, 04:31 PM
need another help......

i have dateStart and dateEnd

both stored as xxxx-xx-xx format in db eg 2009-09-16

i displayed the date in my page as 16 Sep 09




echo "" . date('j M y', strtotime($row['dateStart'])) . "";

echo " - ";

echo "" . date('j M y', strtotime($row['dateEnd'])) . "";


example the date duration is 16 Sep 09 - 18 Sep 09


i want to display it as 16 - 18 Sep 09


and if the date is 29 Jan 09 - 3 Feb 09

i want to display it as 29 Jan - 3 Feb 09

is this possible ?

sabahan
09-17-2009, 12:10 AM
ok solved




$month1 = date('M', strtotime($row['dateStart']));
$month2 = date('M', strtotime($row['dateEnd']));

if ($row[dateEnd]=='0000-00-00')
{
echo date('j M y', strtotime($row['dateStart']));
}


else if ($month1 == $month2) {

echo date('j', strtotime($row['dateStart'])) . ' - ' . date('j M y', strtotime($row['dateEnd']));

} else {

echo date('j M y', strtotime($row['dateStart']) . ' - ' . date('j M y', strtotime($row['dateEnd']);

}