PDA

View Full Version : TimeStamp


simjay
07-30-2005, 09:38 PM
Hello

I have got a mysql database that has automatic timestamp in the table.

In the php page i would like to display the time stamp:

My code
<? echo "$date_time"; ?>

How can i get it to display 2005/07/30 19:02:46 insted of 20050730190246

Many thanks

mark87
07-30-2005, 10:05 PM
$thetime = date(Y/m/d H:i:s);

<? echo "$thetime"; ?>

I think. :)

Fou-Lu
07-30-2005, 10:25 PM
Yep, but...

$thetime = date('Y/m/d H:i:s', $date_time);

There, now its all good, that will format for your timestamp instead of the current time.

marek_mar
07-31-2005, 02:16 AM
You should know that the timestams that are stored in type "timestamp" fields are not the same type of timestam as PHP uses.

dumpfi
07-31-2005, 02:38 AM
You can easily convert the timestamp to a more readable date:
function mySQLDate($time) {
$parts = str_split($time, 2);
return $parts[0].$parts[1].'/'.$parts[2].'/'.$parts[3].' '.$parts[4].':'.$parts[5].':'.$parts[6];
}

echo mySQLDate($date_time);

dumpfi

Fou-Lu
07-31-2005, 11:01 AM
You should know that the timestams that are stored in type "timestamp" fields are not the same type of timestam as PHP uses.

:eek:
Didn't even notice that, lol. I thought you meant a user inserted timestamp. Lol, guess I should pay more attention when reading.

simjay
08-01-2005, 11:17 PM
Yep, but...

$thetime = date('Y/m/d H:i:s', $date_time);

There, now its all good, that will format for your timestamp instead of the current time.


It is displaying the year as 2038 when it is 2005 in the database? Any ideas?

marek_mar
08-01-2005, 11:22 PM
Read the two posts after that post.

simjay
08-01-2005, 11:34 PM
that function thing did not work :(

marek_mar
08-01-2005, 11:40 PM
You will laugh. It works for me.
How are you using it?

<?php
$date = '20050730190246';
function mySQLDate($time) {
$parts = str_split($time, 2);
return $parts[0].$parts[1].'/'.$parts[2].'/'.$parts[3].' '.$parts[4].':'.$parts[5].':'.$parts[6];
}

echo mySQLDate($date);
// echos 2005/07/30 19:02:46
?>

CrzySdrs
08-02-2005, 05:54 PM
Or perhaps a simpler solution:


$thetime = date('Y/m/d H:i:s', strtotime($date_time));

marek_mar
08-02-2005, 06:27 PM
strtotime() will return -1 when you try to use it with a mysql timestamp.
Valid date input formats: http://www.gnu.org/software/tar/manual/html_chapter/tar_7.html

CrzySdrs
08-02-2005, 06:29 PM
My mistake, I usually use the DateTime field in mysql. Makes for easier days with the strtotime function.

simjay
08-02-2005, 08:36 PM
You will laugh. It works for me.
How are you using it?

<?php
$date = '20050730190246';
function mySQLDate($time) {
$parts = str_split($time, 2);
return $parts[0].$parts[1].'/'.$parts[2].'/'.$parts[3].' '.$parts[4].':'.$parts[5].':'.$parts[6];
}

echo mySQLDate($date);
// echos 2005/07/30 19:02:46
?>



i get this:

Fatal error: Call to undefined function: str_split() in /home/corner/public_html/view.php on line 62

marek_mar
08-02-2005, 10:32 PM
Ok that makes sense. You're not using PHP5 right (I do on my test server/debugger)?
I think it could be done in some better way but it works.

<?php
$date = '20050730190246';
function mySQLDate($time)
{
$parts = explode(',', chunk_split($time, 2, ','));
return $parts[0].$parts[1].'/'.$parts[2].'/'.$parts[3].' '.$parts[4].':'.$parts[5].':'.$parts[6];
}

echo mySQLDate($date);
// echos 2005/07/30 19:02:46
?>