Page 1 of 1
obtain just date for display from mysql timedate
Posted: Sat Dec 05, 2009 6:19 am
by Ben11858
Hey guys..
Just wondering, I have a mysql database with a date row that uses current datetime stamp for each row that is entered.
When I display it on my php page I want to only display the date, is there anyway to seperate the time from the date??
Cheers..
Re: obtain just date for display from mysql timedate
Posted: Sat Dec 05, 2009 12:08 pm
by askbapi
Turn the date and time stamp into string and use substr() function to through out , what u don't want.
This is one way, There must better way too.
Re: obtain just date for display from mysql timedate
Posted: Sat Dec 05, 2009 1:10 pm
by AbraCadaver
Ben11858 wrote:Hey guys..
Just wondering, I have a mysql database with a date row that uses current datetime stamp for each row that is entered.
When I display it on my php page I want to only display the date, is there anyway to seperate the time from the date??
Cheers..
Code: Select all
$new_date = date($format, strtotime($datetime_from_db));
-Shawn
Re: obtain just date for display from mysql timedate
Posted: Sat Dec 05, 2009 3:53 pm
by daedalus__
EHT EHT WAIT
http://dev.mysql.com/doc/refman/5.4/en/ ... _timestamp
$sql = "SELECT TIMESTAMP(datetime) FROM yola WHERE pure = 1"
$res = mysql_query($sql);
$timestamp = mysql_result($res, 0);
$new_date = date($format, $timestamp);
Re: obtain just date for display from mysql timedate
Posted: Sun Dec 06, 2009 7:30 am
by Ben11858
Thanks for that.. The date function works ok when i tested it out..
But when i put in the timestamp from mysql the only date i get out is 01-01-70 for some reason..
Here is a snippet to show you what i mean.
Code: Select all
$select = "SELECT date, status, afirstname, alastname FROM case WHERE id= '11'";
$result = mysql_query($select);
$row= mysql_fetch_array($result)
$date = $row['date'];
echo $date;
$dateformat = date('j-n-y',$date);
echo $dateformat;
I assigned $row['date'] to $date to echo it to see what output I recived which was date as per mysql with time as well so checking that query worked etc.. So I put it through the date() with my string so I get the day-month-year and echo it to see results. This is what I get
2009-12-01 12:34:08 ($date one)
1-1-70 ($dateformat one)
Any ideas why I get this result and not 01-12-09 like I should get?
Re: obtain just date for display from mysql timedate
Posted: Sun Dec 06, 2009 11:10 am
by AbraCadaver
PHP date() expects a unix timestamp, so you need to strtotime() the date you pull from the database. What daedalus__ posted won't work because the mysql TIMESTAMP() is not the unix timestamp that PHP date() needs. So either do it the way I showed in PHP, or you can do it in the database like:
Code: Select all
SELECT DATE(`date`), `status`, `afirstname`, `alastname` FROM `case` WHERE `id` = 11
Or use DATE_FORMAT(`date`) in place of DATE() in your query, then you can format it how you want.
-Shawn
Re: obtain just date for display from mysql timedate
Posted: Sun Dec 13, 2009 6:35 am
by Ben11858
Awesome, cheers for that mate.. I had left that for a while and moved on thinking I'd get back to it cause I couldn't figure away around it.. But today I needed to pull a date from the database and format it into a different format and came into the same problem... Thank god I came back to this thread again, the strtotime() worked a treat, cheers saved me heaps of time with that little one..