Jump to content

[SOLVED] How to format - Mysql 5 timestamp


Alexhoward

Recommended Posts

Good morning PHP people!

 

I am using an auto timestamp in mysql 5.

 

This gets added automatically when the data is inserted

 

It's saved as:

 

2008-06-16 09:09:53

 

and i would like to change it to:

 

Mon 16 June

 

problem is that when i use the normal php date formatting method it comes out with some bizare results...

 

has anyone come across this before...?

 

Thanks

Link to comment
Share on other sites

Leave it alone in the database - that format is the best.

 

Change how it is displayed by your scripts after being retrieved from the database.

 

The date() function allows all kinds of personal preference date formats.

Link to comment
Share on other sites

Hi Guys,

 

Thanks for the replies!

 

here's my code: (just a test and an extract of the full thing)

<?php 


include("config.php");

//connect to the mysql server
$link = mysql_connect($host, $db, $pass)
or die ("Could not connect to mysql because ".mysql_error());

//select the database	
mysql_select_db($db)
or die ("Could not select database because ".mysql_error());

$query2=" SELECT * FROM mail ";
$result2=mysql_query($query2);
echo mysql_error();

while($row = mysql_fetch_array($result2)) { 
    $from = $row['sender'];
    $title = $row['title'];
    $body = $row['body'];
    $date = $row['date'];
    $ref = $row['ref'];
}

echo "$date</br>";

echo date("D-d-M-Y", $date);


?>

 

problem being that my output is:

 

2008-06-16 09:26:23

Thu-01-Jan-1970

 

any ideas why this is happening...?

 

Thanks

Link to comment
Share on other sites

sorry ...i ve misunderstood....

try this ...it satisfies ur expectations...

while($row = mysql_fetch_array($result2)) { 
    
    $date = $row['created_at'];
    $ref = $row['ref'];$date=date('D-M-Y');

Link to comment
Share on other sites

Excellent!

 

That seems to work a treat!

 

Thanks

 

Just a quick question thou, as the two variables have the same name

 

i.e.

 

$date = $row['date'];

$date=date('D d/M/Y');

 

and i'm echo-ing  $date

 

is it just combining them...?

Link to comment
Share on other sites

The date() function expects the second parameter to be a UNIX timestamp (integer), you are passing it a string, so it gives you back the zero date (1/1/1970). You need to use the function strtotime() to convert the string date stamp to a UNIX timestamp before you use it with the date() function.

 

<?php
echo "$date</br>";

echo date("D-d-M-Y", strtotime($date));

?>

 

or use zenag's method.

 

Ken

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.