I am trying to convert any timezone to LONDON timezone. At this moment the daylight concept is not considered.
When my time is : 07-04-2016 03:00 PM expected in LONDON is : 07-04-2016 10:30 AM
In my case it is : 07-04-2016 09:30 AM
Here is my php code in CI Helper:
function convert_datetime_to_utc_timezone($date, $timezone) {
if ($date != '') {
$ci = & get_instance();
$dformat .= "Y-m-d H:i:s";
$zone = get_list_of_all_timezone();
$user_time_zone = $zone[$timezone];
$convert_date = new DateTime($date, new DateTimeZone($user_time_zone));
$convert_date->setTimeZone(new DateTimeZone('UTC'));
return $convert_date->format('Y-m-d H:i:s');
} else {
return '';
}
}
Note: $user_time_zone = 'Asia/Calcutta';
If you want to convert your local time to London time, then please use 'Europe/London'. The general time zones like UTC, EST etc wouldn't consider day light saving time.
$your_date = date ( 'Y-m-d H:i:s', strtotime ( $your_date ) );
$newyork_time = new DateTime ( $your_date, new DateTimeZone ( 'America/New_York' ) );
$london_time = new DateTime ( $your_date, new DateTimeZone ( 'Europe/London' ) );
If you want London time, then set the timezone to 'Europe/London', not UTC.
$ php -a
Interactive mode enabled
php > $format = 'd-m-Y H:i A';
php > $input_timezone = 'Asia/Calcutta';
php > $input_datetime = '07-04-2016 03:00 PM';
php > $datetime = DateTime::createFromFormat($format, $input_datetime, new DateTimeZone($input_timezone));
php > $datetime->setTimeZone(new DateTimeZone('Europe/London'));
php > var_dump($datetime->format($format));
string(19) "07-04-2016 10:30 AM"
Related
I am trying to display an event time in three different timezones, so I set the time in a text field ("2:00 pm") with by default is EDT, and the output should be:
2:00 pm EDT / 1:00 pm CDT / 11:00 am PDT
But instead it's just showing as:
2:00 pm EDT / 2:00 pm CDT / 2:00 pm PDT
So, the times are not converting, but the timezone is. Here is my code:
// Get the value from the text field
$value = '2:00 pm';
// Let's first check to see if the value is a valid time
if( strtotime( $value ) ){
// Let's convert the time into an actual time, using today's date as filler
date_default_timezone_set( 'America/New_York' );
$time = date( 'Y-m-d g:i:s a', strtotime( 'today '.$value ) );
// List the timezones we want to return
$timezones = [
'US/Eastern',
'America/Chicago',
'America/Los_Angeles',
];
// Empty array
$display = [];
// Cycle through each timezone
foreach( $timezones as $timezone ) {
// Let's set the timezone
date_default_timezone_set( $timezone );
// Get the time in the new timezone
$new_time = date( 'g:i a T', strtotime( $time ) );
// Make the initials lowercase for the class
$ini = strtolower( date( 'T', strtotime( $value ) ) );
// Add the time to the array
$display[] = '<span class="'.$ini.'-time">'.$new_time.'</span>';
}
// Return all of the times from the array
return implode(' <span class="sep-time">/</span> ', $display );
} else {
// Else state it's not valid
return '<strong>INVALID TIME FORMAT - PLEASE USE "H:MM AM/PM"</strong>';
}
You can see that I'm getting the timezone initials (EDT, CDT, PDT) from the date() function, which all change just fine, but the actual times are not. I tried switching to 'H:i' instead of 'g:i', but it just changes to 7pm, 6pm and 4pm, which are all 5 hours ahead.
my way to change timzones:
$time = new DateTime("now", new DateTimeZone("UTC");
$time->setTimezone(new DateTimeZone("US/Eastern")); // now it's US/Eastern
$time->setTimezone(new DateTimeZone("America/Chicago")); // now it's America/Chicago
make sure you type hint:
use DateTime, DateTimeZone;
I want to check the date input and convert it to 24h format. For example the datetime is:
$date="2021-02-5 11:45:00 AM"
and after i check it with this condition:
if (preg_match("/am|pm|AM|PM/", $date))
{
$date=date('Y-m-d H:i:s',strtotime("$date"));
}
and my output should be like this however it does not return a date format like that:
$date="2021-02-5 11:45:00";
How can i fix this problem?
Converting time format
// 24-hour time to 12-hour time
$time_in_12_hour_format = date("g:i a", strtotime("13:30"));
// 12-hour time to 24-hour time
$time_in_24_hour_format = date("H:i", strtotime("1:30 PM"));
You don't need regex to check if it is 24 hours or 12 hours. you can simply pass the date to strtotime function to get a timestamp of the date and then format the timestamp with date function.
$date="2021-02-5 11:45:00 AM";
$date=date('Y-m-j H:i:s',strtotime($date));
echo $date; // 2021-02-5 11:45:00
date_create_from_format('Y-m-j H:i:s A', $date);
It will return a DateTime object, and you can get right date you want, and it will return false if date string don't match the pattern.
DateTime Object
(
[date] => 2021-02-05 11:45:00.000000
[timezone_type] => 3
[timezone] => UTC
)
date_create_from_format ( string $format , string $time , DateTimeZone $timezone = ? ) : DateTime
With strtotime you have no control over the formats. To be on top of what is happening better use the feature-rich DateTime class. First convert the string to a DateTime object and then format it to whatever you need.
<?php
$dt = "2021-02-5 11:45:00 pm";
if (preg_match('/am$|pm$/i', $dt)) // is there an Ante meridiem/Post meridiem suffix?
{
$dtFormat = 'Y-n-j H:i:s a';
}
else
{
$dtFormat = 'Y-n-j H:i:s';
}
$ts = DateTime::createFromFormat($dtFormat, $dt);
$date = $ts -> format('Y-m-d H:i:s');
// echo 'Result: '. $date;
I am converting 08/22/2015 10:56 PM CST to UTC
Conversions
10:56PM CST = 3:56 AM UTC
Therefore the date should be: 2015-08-23 03:56:00 +04
The code below is giving me: 2015-08-23 04:56:00 +04 (which is incorrect)
Code in Repl
https://repl.it/repls/LovelyKhakiExtension
$datetime = '08/22/2015 10:56 PM';
$tz_from = 'CST';
$tz_to = 'UTC';
$format = 'Y-m-d H:i:s +h';
$dt = new DateTime($datetime, new DateTimeZone($tz_from));
$dt->setTimeZone(new DateTimeZone($tz_to));
echo $dt->format($format) . "\n";
My linux box is set to use UTC. Given a timezone and a date, I want to get the date range so that I can query the database for records created on any given day. For instance, if it is now 2018-03-24 at 9am in America/Denver timezone. I want to get the start and end times for this date in UTC. How can I get the UTC equivilant of the beginning of that date?
<?php
$date = new DateTime(date('Y-m-d H:i:s'), new DateTimeZone('America/Denver'));
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('Y-m-d 00:00:00');
?>
this returns 2018-03-24 00:00:00 which is not correct. Any pointers?
Try using this function.
function UTCTimeToLocalTime($time, $tz = '', $FromDateFormat = 'Y-m-d H:i:s', $ToDateFormat = 'Y-m-d H:i:s')
{
if ($tz == '')
$tz = date_default_timezone_get();
$utc_datetime = DateTime::createFromFormat($FromDateFormat, $time, new
DateTimeZone('UTC'));
$local_datetime = $utc_datetime;
$local_datetime->setTimeZone(new DateTimeZone($tz));
return $local_datetime->format($ToDateFormat);
}
echo UTCTimeToLocalTime('2015-07-01 13:30:00','America/Denver');
function LocalTimeToUTCTime($time, $tz = '', $FromDateFormat = 'Y-m-d H:i:s', $ToDateFormat = 'Y-m-d H:i:s')
{
if ($tz == '')
$tz = date_default_timezone_get();
$utc_datetime = DateTime::createFromFormat($FromDateFormat, $time, new
DateTimeZone($tz));
$local_datetime = $utc_datetime;
$local_datetime->setTimeZone(new DateTimeZone('UTC'));
return $local_datetime->format($ToDateFormat);
}
You feed the DateTime constructor with a bogus local time:
new DateTime(date('Y-m-d H:i:s'), newDateTimeZone('America/Denver'));
^^^^
You tell PHP that's a Denver local time but you don't really know. Since the string does not contain time zone information PHP will use the default time zone.
Just drop date(). It serves no purpose and only makes things harder.
<?php
$date = new DateTime('now', new DateTimeZone('America/Denver'));
echo $date->format('r'), PHP_EOL;
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('r'), PHP_EOL;
Sun, 25 Mar 2018 06:11:21 -0600
Sun, 25 Mar 2018 12:11:21 +0000
I have a variable like this,
$Timestamp = "Tue Mar 8 15:59:00 UTC-05:00 2016";
How do I change its format to YYYYMM-DD HH:MM AM/PM and change the timezone from UTC to Pacific Time using PHP?
PHP's DateTime object is pretty flexible.
$UTC = new DateTimeZone("UTC");
$TZ = new DateTimeZone("America/New_York");
$date = new DateTime( "2016-03-09 15:00:00", $UTC );
$date->setTimezone( $TZ );
echo $date->format('Y-m-d H:i:s');
Use php date: date('Y/m/d H:i', $timestamp) but your timestamp should be int:
$timestamp = strtotime('22-09-2008');
You can try following
$Timestamp = "Tue Mar 1 15:59:00 UTC-05:00 2016";
$datetime = new DateTime($Timestamp);
$datetime->format('Y-m-d H:i:s') . "\n";
$new_time = new DateTimeZone('Pacific/Apia');
$datetime->setTimezone($new_time);
//New formatted time
echo $datetime->format('Y-m-d H:i:s');