Forum Moderators: coopster

Message Too Old, No Replies

Dropping the Time from a timestamp

I only wish to display the date

         

dainstructor

4:51 pm on Mar 7, 2007 (gmt 0)

10+ Year Member



I am looking to display the date in a field but I can't figure out how to make the timestamp drop the hours:minutes:seconds. My timestamp originally comes in formatted like "2006-01-01 00:00:00" and I wish to have it read "2006-01-01" only. I've tried using the
 echo (date("Y-m-d",$row_getProject['EndDate'])) 

but it returns an error that reads:

Notice: A non well formed numeric value encountered in C:\htdocs\admin\EditProjects.php on line 109
1969-12-31

and it adds the date of 1969-12-31 for some reason. Does anyone know how I might be able to solve this issue?

milanmk

5:25 pm on Mar 7, 2007 (gmt 0)

10+ Year Member



$a = explode(' ', $row_getProject['EndDate']);

print $a[0];

Milan

whoisgregg

5:26 pm on Mar 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The date function expects a UNIX timestamp, not a date string. Use:

 echo date("Y-m-d", strtotime($row_getProject['EndDate']));

Added: In the vein of string manipulation, you could also use a substring:

echo [url=http://php.net/substr]substr[/url]($row_getProject['EndDate'], 0, 10);

dainstructor

6:00 pm on Mar 7, 2007 (gmt 0)

10+ Year Member



Perfect!

Whoisgregg, I took your advise and used the strtotime(). The only small problem I encountered was that it was still returning the December 31st 1969 date if the date was empty so I placed the function inside of an if statement that checked to see if it was empty first.

 if (isset($row_getProject['BeginDate'])){ echo (date("Y-m-d", strtotime($row_getProject['BeginDate'])));} 

Works well.

Thanks again
-Acton