Forum Moderators: coopster

Message Too Old, No Replies

date and time

         

yllai

6:01 am on Jul 5, 2004 (gmt 0)

10+ Year Member



i have query value for datetime from table A. the result is 2004-07-05 11:05:08

now i want to use php script to display it as YYYY-MM-DD..i have tried with below script but i get the output as 1970-01-01..this is my script:

$date2=date("Y-m-d", mktime(0,0,0,date("Y",$datetime),date("m",$datetime),date("d",$datetime)));
echo"$date2";

i want it get my output as 2004-07-05, any idea? any example?

carneddau

8:47 am on Jul 5, 2004 (gmt 0)

10+ Year Member



Hi,

There are a couple of ways of doing this:

1. Use strtotime():
$date_query_result = "2004-07-05 11:05:08";
$date2 = date("Y-m-d", strtotime($date_query_result));
echo $date2;

date() expects a timestamp as it's second arugment.

2. use explode():
$date_query_result = "2004-07-05 11:05:08";
$date2 = explode(" ", $date_query_result);
echo $date2[0];

coopster

12:37 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Or you could read it and format it during your query.
SELECT DATE('2004-07-05 11:05:08') AS mydate;
Replace the date string here with your table column name, like this:
SELECT DATE(mydate) AS mydate FROM table;

WhosAWhata

3:29 pm on Jul 5, 2004 (gmt 0)

10+ Year Member



$date = "2004-07-05 11:05:08";
$formatted = substr($date,0,strpos($date," "));