Jump to content

Recommended Posts

Hi all. I'm having some issues with time/date conversions. I'm pulling the original info from the metadata of photos, for example 2007:12:15 20:53:37

 

I'm then using strtotime() to convert it to a timestamp and store it in the db. Later on, I use date() to pull it back out and display it in a more friendly format, like Dec 15th, 2007, 8:53 pm.

 

When I do this conversion in a test page, ie:

$time="2007:12:15 20:53:37";
echo ("time is ".$time."<br>");
$time=strtotime($time);
echo ("time is ".$time."<br>");
$time=date("D F j, Y, g:i a",$time);
echo ('time is '.$time);

then it works fine, I get back the same time that I put in, in a nicer format. When I try to do it on my page, though, after letting the timestamp ferment in the database for a bit, I'm getting back a time that is off by 2 hours later, 10:53pm.

 

I've picked through the code on both pages and can't see any reason why it wouldn't be the same as the test. Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/82927-timedate-conversion-problems/
Share on other sites

When you pull the time from the database, it should be a unix timestamp (based on how you say it was stored). After assigning it a variable, say $row['timestamp'], you would go:

 

<?php
// we have $row['timestamp']
$time = date("D F j, Y, g:i a", $row['timestamp']);
echo 'Time is: ' . $time;

 

PhREEEk

The date() function uses the timezone your server is in. You could set the default time zone that php uses, but a better way would be to avoid using the date() function.

 

I recommend storing the the date/time information in your database using a DATETIME data type (YYYY-MM-DD HH:MM:SS) and then use the mysql DATE_FORMAT() function in your SELECT query to output the date in the format you want.

 

This will avoid doing any conversion that is dependent on time zones and will result in the least amount of php code and the quickest execution.

it's stored as TEXT in the database. I had originally intended for 'unknown' to be a possibility when metadata was not available.

 

Well I'm rather confused....

 

I'm then using strtotime() to convert it to a timestamp and store it in the db.

 

If you are doing this, then it is in Unix format. strtotime() converts a string representation of a timestamp to a unix timestamp.

 

PhREEEk

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.