I' am trying to covert date time to hours so that today's date and time matches with the database date and time.
date_default_timezone_set("Pacific/Fiji");
today = date('Y-m-d H:i'); // Returns 2014-05-01 11:01
$date = DateTime::createFromFormat('Y-m-d h:i', '2014-05-02 11:05');
echo $date->format('Y-m-d H:i'); // Returns 2014-05-02 11:05
What am I doing wrong in this code that its returning 2014-05-02 11:05 whereas it should return 2014-05-01 23:05
You saved 2014-05-02 11:05 in Fiji timezone to $date. Afterwards you display the date in Fiji timezone, so of course no change is made.
I don't really understand your question but from the code you provide I guess that what you probably want to do is the following:
$tzFiji = new DateTimeZone('Pacific/Fiji');
$tzBerlin = new DateTimeZone('Europe/Berlin');
$tzCurrent = new DateTimeZone(date_default_timezone_get());
$date = DateTime::createFromFormat('Y-m-d h:i', '2014-05-02 11:05', $tzFiji);
echo $date->format('Y-m-d H:i'); # date in Fiji's timezone
$date->setTimezone($tzBerlin);
echo $date->format('Y-m-d H:i'); # date in Berlin's timezone
$date->setTimezone($tzCurrent);
echo $date->format('Y-m-d H:i'); # date in php's currently set timezone
Related
I have spent too many days & nights reading through documentation and trying just about anything to get this to work.
I have implemented this datetime picker for a website's contact form so users can make reservations and in turn the server will send an .ics calendar request to the owner for his records:
https://mugifly.github.io/jquery-simple-datetimepicker/
The datetimepicker is configured via jquery.simple-dtpicker.js to have format: 'DD-MM-YYYY hh:mm' for both available languages, en & es for English and Spanish. The reason is the website is Spanish.
Up to here everything works great and I get a datetime value sent through when testing the contact form.
My issue is SIMPLY trying to add 1 hour to the obtained datetime in order to fulfill the required format for the .ics event in PHP.
Instead of adding an hour, it either defaults to the current local time and adds 1 hour or it defaults to 01/01/1970 (Epoch Time) and mangles the time. This is not the same outcome I get when succesfully testing the same code in online php testers.
Here is my php code:
<?php
date_default_timezone_set("Europe/Berlin");
$dateStart = $_POST['ical'];
$date = date_format($dateStart, 'd-m-Y H:i');
$dateZ = date('d-m-Y H:i', $date);
$dateDone = date('d-m-Y H:i', strtotime($dateStart + 3600));
$date1 = date('d-m-Y H:i', strtotime('+1 hours', $dateStart));
$date2 = date('d-m-Y H:i', strtotime('+1 hours', $dateZ));
$date3 = date('d-m-Y H:i', strtotime('+1 hours', $date));
And my results:
$dateStart: 20-04-2021 05:00
$dateDone: 09-04-3620 07:35
$date1: 01-01-1970 02:00
$date2: 01-01-1970 02:00
$date3: 01-01-1970 02:00
Any idea what could be the issue? I cannot use any of the datetime format/modify/add functions which use -> operator for some reason so the only thing that seems to work is adding an hour via string or integer.
Many thanks
Using the classes DateTime, DateTimeZone and DateInterval
$dt = new \DateTime('now', new \DateTimeZone('Europe/Berlin'));
echo 'now: '.$dt->format('Y-m-d H:i:s') . PHP_EOL;
$dt->add(new \DateInterval('PT1H'));
echo 'then : '.$dt->format('Y-m-d H:i:s') . PHP_EOL;
You have made very silly mistakes.
date_default_timezone_set("Europe/Berlin");
$dateStart = '20-04-2021 05:00';
$date = date_format(new DateTime($dateStart), 'd-m-Y H:i'); // date_format need date time .. string were suplied
$dateZ = date('d-m-Y H:i', strtotime($date));
$dateDone = date('d-m-Y H:i:s', strtotime($dateStart + 3600)); // didnt get what exactly you need.. in date function you can add time easily using str to time
$date1 = date('d-m-Y H:i', strtotime('+1 hours'. $dateStart));
$date2 = date('d-m-Y H:i', strtotime('+1 hours'. $dateZ));
$date3 = date('d-m-Y H:i', strtotime('+1 hours'. $date));
I am using this code to generate yesterday's beginning of day in PST (aka America/Los_Angeles) time. I can't figure out how to convert the result to UTC.
date_default_timezone_set("America/Los_Angeles");
$time1 = date("Y-m-d H:i:s", mktime(0,0,0, date('n'), date('j')-1, date('Y')));
I tried this, but $time1 is not datetime, it's string. So the following won't work.
$time1->setTimezone(new DateTimeZone("UTC"));
The DateTime class can do all that for you
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles')); // will use now
echo $date->format('d/m/Y H:i:s'); //16/08/2016 16:13:29
$date->setTime(0,0,0);
$date->modify('-1 day');
echo $date->format('d/m/Y H:i:s'); // 15/08/2016 00:00:00
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('d/m/Y H:i:s'); // 15/08/2016 07:00:00
$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.
In php I have set the follwing
date_default_timezone_set('Asia/Kolkata');
but it is not displaying accurate time.
date_default_timezone_set('Asia/Kolkata');
this is the link of my website http://www.adsnjobs.com/
here I have echo the time zone and current date time by using
echo date_default_timezone_get() . date('Y-m-d h:i:s a');
First, you should use the newer DateTime class, just as good practice:
$date = new DateTime(null, new DateTimeZone('Asia/Kolkata'));
echo $date->format('Y-m-d h:i:s a');
Second, have you tested whether the issue is with your server clock, not the IST time zone? It looks about 10 minutes off to me. Try this:
$date = new DateTime(null, new DateTimeZone('Asia/Kolkata'));
echo $date->format('Y-m-d h:i:s a :: T');
$date->setTimezone(new DateTimeZone('Etc/UTC'));
echo $date->format('Y-m-d h:i:s a :: T');
This will show you the date in UTC and in IST so you can see if there is a 5:30 offset. If there is, it's your server clock.
i am new to php.
i want to write a function where i need user to input date in any date format including DST,into GMT format and again later back into the original entered format.please any body help me.
Although the gmdate functions are available. If you are using PHP 5.2 or greater, then consider using the DateTime object.
Here's code to switch to GMT
$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT'));
and back to the default timezone...
$date = new DateTime('2011-01-01', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
Using the DateTime object lets your create a datetime, just like the procedural functions, except that you keep a reference to an instance.
e.g.
// Get a reference to Christmas of 2011, at lunch time.
$date = new DateTime('2011-12-25 13:00:00');
// Print the date for people to see, in whatever format we specify.
echo $date->format('D jS M y');
// Change the timezone to GMT.
$date->setTimezone(new DateTimeZone('GMT'));
// Now print the date/time it would in the GMT timezone
// as opposed to the default timezone it was created with.
echo $date->format('Y-m-d H:i:s');
// Just to show of some more, get the previous Sunday
$date->modify('previous Sunday');
There's a whole lot of functions you can use, that are much more readable that the procedural functions.
Explicit example of converting from a timezone to GMT
$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');
$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.
echo '<br/>';
// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');
Using your Asia/Kolkata to America/New_York
date_default_timezone_set('Asia/Kolkata');
$date = new DateTime('2011-03-28 13:00:00');
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format("Y-m-d H:i:s");
//Outputs: 2011-03-28 03:30:00
Use the gmdate function to convert to GMT time.
For example
$d = '2011-03-28 12:05:20';
$gmt = gmdate('Y-m-d H:i:s',strtotime($d));
// Convert local time to gmt
public function convertTime($timezone,$time){
$selectedtime = date("Y-m-d H:i",strtotime($time));
$date = new DateTime($selectedtime, new DateTimeZone($timezone));
$date->setTimezone(new DateTimeZone('GMT'));
$convertedtime = strtotime($date->format('Y-m-d H:i'));
return $convertedtime;
}