How do I get a date in UK local time using PHP? - php

I'm using the unix timestamp to show when a message was posted in my project but when I came to displaying the exact time of the post I realized it was about 12 hours behind.
I'm in the UK and my server is in the US (might be the problem).
Is there a simple way of converting the unix timestamp into a readable British time?
$timestamp = time();
print date("F jS, Y", strtotime($timestamp));
Any help would be great,
Thanks!

At the top of your script, write:
date_default_timezone_set('Europe/London');
Or if your PHP is >= 5.2.0:
date_timezone_set('Europe/London');

Just call date_timezone_set with the appropriate parameter for the UK at the start of your script when displaying the dates (not when recording them; I 'm not sure, but it might result in the "wrong" timestamps being recorded).
Edit: The timezone you want is 'Europe/London'.

try date-default-timezone-set.
date_default_timezone_set('Europe/London');

Use date_default_timezone_set('Europe/London'); to set the time zone to London's time. Not sure if it works with summer/winter time.

the simplies way is to substruct gmt offset. e.g:
echo(date('Y-m-d h:i'), $myvalue - 60 * 60 * $nhours));
where $nhours - time defference in hours.

This one worked for me
date_default_timezone_set('Europe/London');

Related

Setting PHP DateTime doesn't appear to be observing TimeZones

I'm fairly new to PHP so forgive me if this is a stupid mistake that I haven't spotted
I've run into a problem where in our current system where we currently used strtotime and it was returning our date an hour ahead than it actually was set. E.g 1:15pm became 2:15pm when I set the timezone to be European rather than GMT.
I read that strotime had this problem but I can't get it to observe a different timezone if I try and set it.
So I tried working with PHPs DateTime instead.
The user enters the time and they select it as 1:15PM however we want to store it as 13:15. So I did this:
$t = DateTime::createFromFormat('h:i A', $venue['startTime']);
$t_24 = $t->format('H:i:s');
Then I try and create my Date object
$d = DateTime::createFromFormat('d-m-Y H:i:s', $venue['startDay'] . ' ' . $t_24);
$d->setTimezone(new DateTimeZone('America/New_York'));
echo ' ' . $d->getTimestamp();
Trying to set the timezone after the object is set because apparently it doesn't work if you add the timezone as the third argument in createFromFormat
My computers time is currently observing European time currently GMT+1 because we're observing daylight savings time in the UK, I select the time set on the through our system as 1:15pm and because I've set the timezone I expect the timestamp outputted equivalent to 7:15am as it's six hours behind European time, however when I convert the timestamp 1500639300 it's still equal to 2:15 PM. Probably done something stupid but can't quite figure out what? Any help would be appreciated :)
Timestamps have no time zone - they are always in UTC time. If you want to save timezone related data use another format! For example save in H:i:s, as you need it.
you can use gmdate() Function for this
<?php $current_time = gmdate('Y-m-d H:i:s'); ?>
http://php.net/manual/en/function.gmdate.php

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.
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)
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;
?>
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/
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");

PHP Winter Time in Europe/Athens

in the php.ini I defined the timezone to Europe/Athens. Everything was just fine until last sunday, when the time chanegd to WINTER TIME. The time went back in 1 hour.
The problem is, that in my website - it's still like summer time, didn't go back in 1 hour... I checked it in other website and it's ok there - http://www.timeanddate.com/worldclock/city.html?n=26
To make sure, I added this line in the top of the page:
ini_set('date.timezone', 'Europe/Athens');
But it dind't help...
What heppent? How can I fix it?
I think this will help you.
date_default_timezone_set('Europe/Athens');
If you just set the default time in the start of your function or program the date is format in the country you want.
<?php
date_default_timezone_set('Europe/Athens') ;
echo "the date is:". date("d/m/Y")."<br/>";
echo "the time is:". date("h:i:s");
?>
Just two little hints:
Please check if the output of following code returns your timezone:
$date = new DateTime();
$tz = $date->getTimezone();
echo $tz->getName();
Which function or language do you use to display the time on your website? Perhaps javascript? Then the time comes from the client system.
You might consider updating your PHP time zone database. You can find the latest version here.
However, I checked through the database and it looks like Europe/Athens has been in sync with EU since 1981, which has used the Last Sunday in October since 1996. So even if you have a very old database I can't imagine that it would be incorrect for recent dates.

Change the simple date time format to show time zone

I have a simple date time in the format 10/20 4:30PM. I want it to display in the format
10/24 1:30PM -007. I am saying date('Y-m-d H:i:sT', $time_recieved) assuming T is for timezone. But It is still not giving the time zone. What might be the reason? Is there any other format I am missing?
What you want is O not T I believe.
Check the PHP date page for all the format listings possible.
http://php.net/manual/en/function.date.php
You can have a look at the official manual of PHP : date ();
http://www.php.net/manual/en/function.date.php
I tested that and I used on my server with :
$time = time();
echo date('Y-m-d H:i:s T', $time);
it shows the timezone perfectly. Check $time_received, it might be an erroneous number, try with local time with timeĀ“() function to ensure that it's OK.
Good luck

PHP Datetime Error

I am having an issue with php date() function.
When I save the date in mysql datebase the hours shows 4 hours less than my current time.
My php code is below: $add_date = date("Y-m-d H:i:s");
It saves the time in database as
2011-08-03 07:51:26
But is should show
2011-08-03 13:22:26
Can anybody tell me how to fix it
Thanks
You have to set a valid time zone - it seems you don't have an appropriate time zone set up in php environment for your location.
Check out
http://www.php.net/manual/en/function.date-default-timezone-set.php
It should give you a clue how to set up the correct time zone in php.
#Mujahid If the printed time and the saved time are the same it probably means your server is not in the same timezone as you. With that said, you have to manually set the default timezone for your PHP script at the top of the file, or get the DateTime by defining your timezone explicitly. Here's the code:
$dateTime = new DateTime("now", new DateTimeZone('America/Los_Angeles'));
$add_date = $dateTime->format("Y-m-d H:i:s");
echo $add_date;
Here's a list of time zones that PHP supports, just find yours and replace America/Los_Angeles with it.
http://php.net/manual/en/timezones.php
Here's a good tutorial on PHP DateTime and DateTimeZone...
http://ditio.net/2008/06/03/php-datetime-and-datetimezone-tutorial/
Hope this helps. Good luck.
Make the time zone setting of the MySQL server and the web server the same.
this will add hosted server time not local time
$add_date = date("Y-m-d H:i:s");
use strtotime() to add hours and minutes
$newdate = date("Y-m-d H:i:s",strtotime('+ 5 hours 30 minutes'.$row['db_date']));

Categories