Jump to content

Recommended Posts

hi, there.

 

I have a timestamp in my mysql database called timestamp and it'll record the current timestamp. however it also records the time etc how do i use php to take apart these for example if i wanted to echo the date and time in a different order or if i only wanted to echo the date not the time.

 

how is this possible?

 

Thanks for reading.

 

Link to comment
https://forums.phpfreaks.com/topic/235732-timestamps-bit-of-a-newbie-question/
Share on other sites

The problem is, the other person showed you how to do it using MySQL and you said PHP. So, here is how we do this in PHP:

 

<?php

$myDate = '2011-05-06 16:10:30';

echo date('m/d/Y', strtotime($myDate)); // Output: 05/06/2011
echo date('g:iA', strtotime($myDate)); // Output: 4:10PM
echo date('M jS, Y @ g:iA', strtotime($myDate)); // Output: Jan 6th, 2011 @ 4:10PM

?>

 

Basically, you need to take your MySQL result, convert it to a UNIX timestamp using strtotime() then use the date() function to display what you want.

 

For more information: http://php.net/manual/en/function.date.php

jcbones' solution is leaps and bounds better than smsmarketeers from an efficiency and simplified coding standpoint.

 

Yeah well, he said he wanted to do it in PHP, not the MySQL query nor did he ask for "simplified" or "efficient". Not to mention that I would NEVER use MySQL to control dates and times because of timezone settings and other problems. PHP makes a much better effort at controlling times with timezones.

thanks for the quick reply, i've read the manual but i'm a little confused :(

 

when using the php $date=mysql_fetch_object(mysql_query("SELECT DATE_FORMAT(`date`,'%W %M %Y') FROM `updates` WHERE id='1'"));

 

then echo $date it doesn't work :(

 

 

please help.

thanks for the quick reply, i've read the manual but i'm a little confused :(

 

when using the php $date=mysql_fetch_object(mysql_query("SELECT DATE_FORMAT(`date`,'%W %M %Y') FROM `updates` WHERE id='1'"));

 

then echo $date it doesn't work :(

 

 

please help.

 

you want to use something like this:

 

$date= mysql_result(
mysql_query("SELECT DATE_FORMAT(`date`,'%W %M %Y') FROM `updates` WHERE id='1'"),
0
);

 

If you have multiple fields to grab, you can use something like this instead

 

<?php

$data= mysql_fetch_assoc(
mysql_query("SELECT DATE_FORMAT(`date`,'%W %M %Y') as `date`, `another_colum` FROM `updates` WHERE id='1'"),
0
);

print_r( $data );

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.