How do I get a date in UK local time using 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_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
Date/Time Functions - Manual, mktime() - Get Unix timestamp for a date; date() - Format a local time/date I have used GMT time to create the timestamp as there is no offset for UK time (+00 ). Only local time is reported, according to the current TZ environment setting. One could try to use putenv() to set another timezone temporarily, however when running PHP in safe mode, putenv() is disabled and cannot be used in scripts.
gmmktime - Manual, In order to do calculations with date/time information, you should use the more capable DateTimeImmutable. Each parameter of This differs from other versions where it is correctly computed from current time. joe at strtotime dot co dot uk �. Primarily, date () function is used to set the format of date and time, but at the same time, it can be used to get the information about current date and time. There are two parameters used in this function which are described below. (1) $format : This string parameter is used to identify the format of the date and 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.
strtotime - Manual, DateTime::setTimezone -- date_timezone_set — Sets the time zone for the <? php $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru')); You can use PHP date () function or DateTime () class to get current Date & Time in PHP. This tutorial will help you to get current date time in PHP. The provided results based on the timezone settings in the php.ini file. You may need to modify this setting to get date and time in the required timezone.
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.
DateTime::setTimezone - Manual, The optional timestamp parameter in the date () function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above). The PHP mktime () function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Reading the timezone set using the date_default_timezone_set() function (if any) Prior to PHP 5.4.0 only: Reading the TZ environment variable (if non empty) Reading the value of the date.timezone ini option (if set) Prior to PHP 5.4.0 only: Querying the host operating system (if supported and allowed by the OS).
date(string$format[, int$timestamp= time()]) : string Returns a string formatted according to the given format string using the given integer timestampor the current time In other words, timestampis optional and defaults to the value of time().
Optional. Specifies a Unix timestamp that defaults to the current local time, time(), if no timestamp is specified: is_assoc: Optional. Specifies whether to return an associative or indexed array. FALSE = the array returned is an indexed array. TRUE = the array returned is an associative array. FALSE is default. The keys of the associative
- Please stop writing tags in your question titles. You've done it with almost all of your questions.
- Can you show some code? Where are the dates coming from?
- Sorry Tom, didn't realize that was a problem. Thanks for the tip.
- I hope not. Otherwise any script running outside of GMT is in big trouble.
- @TomalakGeret'kal: I don't quite get your meaning. I was thinking that the behavior would be dependent on the system clock going with UTC time vs. the local time. In the second case only PHP would need to convert from local to UTC to get a timestamp; therefore setting the "wrong" local timezone would result in a wrong TS. I might be wrong though.
- Depends what he's doing, I guess.
- Sure, if you want to write this mess every single time you want to work with a date.