obtain just date for display from mysql timedate

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
Ben11858
Forum Newbie
Posts: 16
Joined: Sat Nov 28, 2009 11:27 am

obtain just date for display from mysql timedate

Post 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..
askbapi
Forum Newbie
Posts: 10
Joined: Fri Dec 04, 2009 11:24 pm
Location: India

Re: obtain just date for display from mysql timedate

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: obtain just date for display from mysql timedate

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: obtain just date for display from mysql timedate

Post 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);
Ben11858
Forum Newbie
Posts: 16
Joined: Sat Nov 28, 2009 11:27 am

Re: obtain just date for display from mysql timedate

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: obtain just date for display from mysql timedate

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Ben11858
Forum Newbie
Posts: 16
Joined: Sat Nov 28, 2009 11:27 am

Re: obtain just date for display from mysql timedate

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