This timezone problem is driving me crazy and hoping someone can put me in my place fast as it's likely super simple
$timezone = 'EST';
$datetimenow = new DateTime("Now", new DateTimeZone($timezone));
$today = $datetimenow->format("m/d/Y"); // = 03/26/2021
// FOR LOOP
$filter = $today .' '.$details->data->time['start']; // where start = 8:30 PM
// END FOR LOOP
$datetimeevent = new DateTime($filter, new DateTimeZone($timezone));
// datetimeevent print_r = correct =
DateTime Object
(
[date] => 2021-03-26 20:30:00.000000
[timezone_type] => 2
[timezone] => EST
)
$filter = $datetimeevent->getTimestamp();
// $filter = 1616808600
// 1616808600 =
//String:
//GMT: Saturday, March 27, 2021 1:30:00 AM
//Your time zone: Friday, March 26, 2021 9:30:00 PM GMT-04:00 DST
huh? how is 8:30 becoming 9:30 on what should be a straight up conversion?
Related
It easier to explain in example so let just dive into it:
I have a range of date, say, 1st of December 2019 - 1st of Feb 2020.
So my requirement is to get :
Sunday - 01 December - 2019
Monday - 02 December - 2019
Tuesday - 03 December - 2019
Wednesday - 04 December - 2019
...
...
...
...
...
...
Friday - 31 January - 2020
Saturday - 01 February - 2020
in an array of string in PHP.
I think you are looking for some thing like this
function getDatesFromRange($start, $end, $format = 'l - d F - Y') {
// Declare an empty array
$array = array();
// Variable that store the date interval
// of period 1 day
$interval = new DateInterval('P1D');
$realEnd = new DateTime($end);
$realEnd->add($interval);
$period = new DatePeriod(new DateTime($start), $interval, $realEnd);
// Use loop to store date into array
foreach($period as $date) {
$array[] = $date->format($format);
}
// Return the array elements
return $array;
}
// Function call with passing the start date and end date
$dates = getDatesFromRange('2010-10-01', '2010-10-05');
echo '<pre>';
print_r($dates);
Output :
Array
(
[0] => Friday - 01 October - 2010
[1] => Saturday - 02 October - 2010
[2] => Sunday - 03 October - 2010
[3] => Monday - 04 October - 2010
[4] => Tuesday - 05 October - 2010
)
You can use strtotime() to increase your date by 1 date:
$start = strtotime("1 December 2019");
$end = strtotime("1 Feb 2020");
$dateArray = [];
while($start <= $end) {
$dateArray[] = date("l - d F Y", $start);
$start = strtotime("+1 day", $start);
}
Please use the below code.
<?php
function getDatesFromRange($start, $end, $format = 'l-d-F-Y') {
// Declare an empty array
$array = array();
// Variable that store the date interval
// of period 1 day
$interval = new DateInterval('P1D');
$realEnd = new DateTime($end);
$realEnd->add($interval);
$period = new DatePeriod(new DateTime($start), $interval, $realEnd);
// Use loop to store date into array
foreach($period as $date) {
$array[] = $date->format($format);
}
// Return the array elements
return $array;
}
// Function call with passing the start date and end date
$Date = getDatesFromRange('2019-12-01', '2020-02-01');
var_dump($Date);
?>
Thanks,Arun
I have an array which contains time values in GMT. :
Array
(
[0] => Array
(
[h] => 5
[m] => 0
)
)
Here Array[0][0] is the array for start time and Array[0][1] is the array for end time.
Now what I am creating time using date time as:
$timezone = new DateTimeZone("Asia/Kolkata"); //Converting GMT to IST.
$startTime = new DateTime();
$startTime->setTime($Array[1][0]["hour"], $Array[1][0]["minute"]);
$startTime->setTimezone($timezone);
$startTime->format('h:i a'); //output 10:30 PM // Should be 10:30 AM
$endTime = new DateTime();
$endTime->setTime($Array[1][1]["hour"], $Array[1][1]["minute"]);
$endTime->setTimezone($timezone);
$endTime->format('h:i a'); //output 10:30 PM //ok
So My $startTime and $endTime both has the same value of `10:30` PM but I want the startTime to have value of `10:00 AM`because its period was `AM` in the array.
When creating new Datetime object You have to specify timezone, because default value is taken from PHP configuration and this may not be set to GMT. So initialization of $startTime and $endTime should be:
$startTime = new DateTime('', new DateTimeZone('GMT'));
$endTime = new DateTime('', new DateTimeZone('GMT'));
Then when You are using setTime() You have to add 12 hours when PM period is taken. It should look like that:
$startTime->setTime($Array[1][0]["hour"] + ($Array[1][0]["period"] === 'PM' ? 12 : 0), $Array[1][0]["minute"]);
$endTime->setTime($Array[1][1]["hour"] + ($Array[1][1]["period"] === 'PM' ? 12 : 0), $Array[1][0]["minute"]);
Rest of the code looks fine, besides in You example $Array[1] is undefined. $Array[0] is set.
You could also use DateTime::createFromFormat():
$d = DateTime::createFromFormat('g:m A', '5:30 PM', new DateTimeZone('GMT'));
$d->setTimeZone(new DateTimeZone('Asia/Kolkata'));
var_dump($d->format('Y-m-d H:i:s'));
Gives:
string(19) "2017-03-16 22:30:00"
You can create the string '5:30 PM' by combining the elements of your array. Please note that you must add a leading 0 to the minutes if they are less than 10, e.g: 9 minutes -> 09
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"
I'm trying to set the time between 2 variables but when I check it copies the change on the 2 time. For example, I select 1:00 and 23:00 , both dates show 23:00 only. I can't seem to get why it's showing like that even if I have already different variables for each time. I need to show the different dates.
Input Dates:
01:30 and 21:00
My code:
$date = $_POST['datepicker'];
$time1 = $_POST['timepicker'];
$time2 = $_POST['timepicker2'];
$time1_array = explode(":",$time1);
$time2_array = explode(":",$time2);
$date = $_POST['datepicker'];
$date = new DateTime($date);
$date2 = $date;
$datefinal1 = date_time_set($date, $time1_array[0], $time1_array[1], 00);
$datefinal2 = date_time_set($date2, $time2_array[0], $time2_array[1], 59);
$result = $datefinal1->format('Y-m-d H:i:s');
$result2 = $datefinal2->format('Y-m-d H:i:s');
print_r($datefinal1);
Sample output that I am getting:
DateTime Object ( [date] => 2015-03-17 21:00:59 [timezone_type] => 3
[timezone] => Europe/Berlin )
even if I should be getting the $datefinal2's value.
Objects are copied by reference, not value. If you assign one object to a new variable both point to the same object so changing one changes the other. To copy an object you need to use clone.
$date2 = $date;
should be:
$date2 = clone $date;
$user_timezone_from = 'UTC';
$user_timezone_to = 'Pacific/Auckland';
$date = new DateTime('2015-08-21 14:00', new DateTimeZone($user_timezone_from));
$date->setTimezone(new DateTimeZone($user_timezone_to));
echo $cur_gmt_date = $date->format('Y-m-d H:i:s');
$date = new DateTime('2015-09-29 14:00', new DateTimeZone($user_timezone_from));
$date->setTimezone(new DateTimeZone($user_timezone_to));
echo $cur_gmt_date = $date->format('Y-m-d H:i:s');
I am getting different results for this.
2015-08-22 02:00:00
2015-09-30 03:00:00 It should be 2015-09-30 02:00:00
Why I got wrong value?
Wikipedia tells me that on the last Sunday of September there's a DST change in New Zealand. Hence a one hour difference to Summer Time.