user3754277
user3754277

Reputation: 225

PHP date showing wrong time despite the timestamp being correct

I'm having a problem with the PHP date function which I've never had a problem with before.

The timestamp is entirely correct, however for some bizarre reason date() is outputting a time which does not correspond.

I have the following timestamp (and this is definitely the correct one - when I echo it out onto the page, as well as in the database, it shows as being correct):

464400

Yet when I use the following line of code:

<?php echo date("H:i",$timestamp); ?>

I'm getting a time of 4 am? If I paste the timestamp into a timestamp converter website, then it shows the time should in fact be 9am.

I'm completely stuck, this has never happened to me before & this problem has only just come up recently - the code hasn't been changed and everything seemed to be working correctly before.

Does anyone have any experience with this issue? Any help would be appreciated.

Upvotes: 15

Views: 47567

Answers (4)

Whiz
Whiz

Reputation: 11

For time zone: https://www.php.net/manual/en/timezones.php

date_default_timezone_set('America/Chicago');

echo date("m/d/Y h:i:s A");

Upvotes: 0

Vijay Balan
Vijay Balan

Reputation: 51

try this method will work

for time zone http://php.net/manual/en/timezones.php

code

<?php
date_default_timezone_set('Asia/Kolkata'); 

$dt2=date("Y-m-d H:i:s");
echo $dt2;

?>

Upvotes: 5

user789456
user789456

Reputation: 173

try this

// set default timezone
date_default_timezone_set('UTC');
//define unix timestamp
$timestamp = 1456778973;
// output
echo date('d M Y H:i:s',$timestamp);

Try this converter too http://freeonlinetools24.com/

Upvotes: 1

exussum
exussum

Reputation: 18550

That time stamp is is 9am GMT timezone, if you are in another timezone then you will need to adjust it accordingly.

http://php.net/manual/en/function.date-default-timezone-set.php

  date_default_timezone_set('Europe/London');

or even better in your php.ini

http://php.net/manual/en/datetime.configuration.php

date.timezone="Europe/London"

Or you can use more specifically GMT instead of Europe/London (which has DST)

Upvotes: 19

Related Questions