Jump to content

[SOLVED] Using mysql timestamp with php


Stealth549

Recommended Posts

I wasn't sure where to post this either this section or the mysql section. Anyway I have a automated timestamp within mysql. Now when I use the date function I dont get the result that I was expecting. Getting the incorrect date.

 

this is the code that I am using and the results can be seen at the url below.

date("Y-m-d",$row['date'])

 

http://stealth549.com/dev/news/customer/php/includes/datetest.php

 

Thanks for any help.

Link to comment
Share on other sites

The second parameter of the date() function is a timestamp. A timestamp is a number in seconds which is the time that has passed since the unix epoch (1/1/1970). Therefore, to change the the format of the date, you would first need to convert the date time field you have to a timestamp, either with the strtotime() function, or with the mysql UNIX_TIMESTAMP() function.

 

However, the mysql DATE_FORMAT() function will take a date time string as its value. This is probably your best option:

 

<?php
$sql = "SELECT DATE_FORMAT( `yourfield` , '%y %c %e' )  FROM `yourtable`";
$result = mysql_query($sql) or die(mysql_error());
?>

Link to comment
Share on other sites

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.