I'm having trouble converting timezone's from UTC to a user selected timezone. The problem seems to be Daylight Savings Time.
Here is an example I just coded.
<?php
date_default_timezone_set("UTC");
$timezone = -5.0;
$timestamp = time();
$local_time = $timezone * 3600 + $timestamp;
echo date( "m/d/Y - h:i A", $local_time );
?>
When I run the test file it returns 07/21/2014 - 04:29 PM. The current time is actually 5:29. The problem is Daylight Savings Time, where our clocks are turned back an hour.
How can I remedy this problem, or is there a more effective method for adjusting timestamps?
Easiest solution I see is making users select whether DST is currently in effect where they live, as not every country/timezone uses DST. If it is in effect then simply modifying the $timezone variable to +1 would suffice, but would require each user to manage whether DST is in effect or not.
Thank-you in advance.
EDIT:
I tried using DateTime but it was still off...
<?php
$timestamp = time();
echo 'Unix timestamp: ' . $timestamp . "<br/>";
echo 'Unix date: ' . date( "m/d/Y - h:i A", $timestamp ) . "<br/><br/>";
$dt = date_create_from_format('U', $timestamp);
date_timezone_set($dt, new DateTimeZone('America/New_York'));
$adjusted_timestamp = date_format($dt, 'U') + date_offset_get($dt);
echo 'America/New_York: ' . $adjusted_timestamp . "<br/>";
echo 'America/New_York: ' . date( "m/d/Y - h:i A", $adjusted_timestamp );
?>
The results were off by +2 hours. Returns 7:47 PM, it is currently 5:47 PM.
Unix timestamp: 1405979278
Unix date: 07/21/2014 - 11:47 PM
America/New_York: 1405964878
America/New_York: 07/21/2014 - 07:47 PM
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));
Here I have set the my Centos time Zone.> sudo hwclock – show
Tue 04 Feb 2014 10:23:10 AM AFT -0.596389 seconds
asia/kabul
Now on PHP I want to echo from 10:23:10 AM to 11:23:10 AM
Here I have set my code for the echoing.
<? echo (date('G', time())+5) ;
echo(':00 To ');
echo (date('G', time())+6);
echo(':00'); ?>
Now as result of my above echoing PHP code I get the result of
15:00 To 16:00
But instead I want to get the echo of below or as what ever my HTTP server time is from NOW to 1 hour next.
10:23:10 AM to 11:23:10 AM
You should try strtotime(). Something like this:
echo date('h:i:s A'), ' to ', date('h:i:s A', strtotime('+1 hour'));
echo date('Y-m-d H:i:s', strtotime('now')), ' to ', date('Y-m-d H:i:s', strtotime('+1 hour'));
For date format, please read: http://www.php.net/manual/en/function.date.php
For your format:
echo date('h:i:s A', strtotime('now')), ' to ', date('h:i:s A', strtotime('+1 hour'));
Add to the time and then print the date.
echo date('G',time()+3600);
time() returns number of seconds since the unix epoch, then you add 3600 seconds for 1 hour and you use that in your date.
You can use explode function.
<?PHP
$time = date('G:i:s');
$eTime = explode(':', $time);
$timePlusOneHour = $eTime+1 . ":{$eTime[1]}:{$eTime[2]}";
echo $time . " To " . $timePlusOneHour;
I want to store the expiration time in database. I am using the below code to store expiration time with +1 year.
$cdate = time();
$date = $cdate + 365*24*60*60;
$date = date("Y-m-d H:i:s",$date);
but its not storing the correct time it stores 2014-08-10 07:55:14 but time on storing is 2014-08-10 01:25:14.
Aslo not sure its Am or Pm .
Thanks.
Time/date functions in PHP are using timezones to determine your local time. So if your server is in timezone GMT+6 that means that the date() function will return you the date/time that is 6 hours before GMT.
You can check the date_default_timezone_set() function manual to find out how PHP is selecting your timezone.
To set your timezone, you can use date_default_timezone_set() before calling date function or you can set you php.ini setting date.timezone to your timezone.
For the second part of your question - when formatting time using the date() function the H format character will return 24-hour format of an hour with leading zeros.
try this
<?php
$timezone1 = "America/Los_Angeles";
date_default_timezone_set($timezone1);
$cdate = time();
$date1 = $cdate + 365*24*60*60;
$date = date("Y-m-d H:i:s a",$date1);
echo $date;
$timezone = "Asia/Calcutta";
date_default_timezone_set($timezone);
$cdate = time();
$date1 = $cdate + 365*24*60*60;
$date = date("Y-m-d H:i:s a",$date1);
echo $date;
?>
you can set timezone for your location.And also refer this codepad-FIDDLE
As others have mentioned, it is calculating the time based on your server (local) time.
I suggest you store the time in GMT and then adjust it to your desired timezone as necessary.
You can use strtotime() to calculate 1 year from now (no need to calculate it yourself) and use gmdate() to get the timestamp in GMT.
echo "Next Year in local time: ". date("Y-m-d H:i:s", strtotime("+1 year")) ."\n";
echo "Next year in GMT: " . gmdate ("Y-m-d H:i:s", strtotime ("+1 year")) . "\n";
// Output:
// Next Year in local time: 2014-08-10 15:25:09
// Next year in GMT: 2014-08-10 08:25:09
If I have a random unix timestamp, how can I round it down to today's midnight or the midnight selected by the user. The reason for this is that I want to add hours and minutes after a certain day's midnight.
For example if the timestamp is 1324189035 then how can I remove the hours, minutes, and seconds to put the timestamp at midnight for that day.
echo date('d-m-Y H:i:s', strtotime('today', 1324189035));
Because of how you're using it, I wouldn't calculate midnight at all: it is far easier to simply convert what you're adding to the timestamp into 24 hour time and then use strtotime:
echo strtotime("0:00",1324189035); // 1324184400
echo strtotime("17:50",1324189035); // 1324248600
And if you want to have that in human readable, use date and m/d/Y H:i:s:
echo date('m/d/Y H:i:s', strtotime('17:50',1324189035)); // 12/18/2011 17:50:00
Simply Use
strtotime('today midnight');
Just do
date('d-m-Y',strtotime('today'));
Easy!
An easy solution would be to use the modulo expression to remove the exceeded seconds from a round day timestamp.
<?php
date_default_timezone_set('UTC');
$timestamp = time();
echo "timestamp : " . $timestamp . PHP_EOL;
echo "timestamp formatted : " . date('Y-m-d H:i:s', $timestamp) . PHP_EOL;
$diff = $timestamp % (60 * 60 * 24);
echo "diff : " . $diff . PHP_EOL;
$midnight = $timestamp - $diff;
echo "midnight : " . $midnight . PHP_EOL;
echo "midnight formatted : " . date('Y-m-d H:i:s', $midnight) . PHP_EOL;
This would output the following result.
timestamp : 1575451074
timestamp formatted : 2019-12-04 09:17:54
diff midnight : 1575417600
midnight formatted : 2019-12-04 00:00:00
And here is a one liner function to get your midnight from any timestamp.
function getMidnight ($timestamp) { return $timestamp - ($timestamp % 86400); }
http://sandbox.onlinephpfunctions.com/code/1d85c935e71fcf011284ae33658e0c68dd8d8c28
How about just:
date -d $(date +%F) +%s
I get the time:
$today = time();
$date = date('h:i:s A', strtotime($today));
if the current time is "1:00:00 am", how do i add 10 more hours to become 11:00:00 am??
strtotime() gives you a number back that represents a time in seconds. To increment it, add the corresponding number of seconds you want to add. 10 hours = 60*60*10 = 36000, so...
$date = date('h:i:s A', strtotime($today)+36000); // $today is today date
Edit: I had assumed you had a string time in $today - if you're just using the current time, even simpler:
$date = date('h:i:s A', time()+36000); // time() returns a time in seconds already
$tz = new DateTimeZone('Europe/London');
$date = new DateTime($today, $tz);
$date->modify('+10 hours');
// use $date->format() to outputs the result.
see DateTime Class
(PHP 5 >= 5.2.0)
You can simply make use of the DateTime class , OOP Style.
<?php
$date = new DateTime('1:00:00');
$date->add(new DateInterval('PT10H'));
echo $date->format('H:i:s a'); //"prints" 11:00:00 a.m
$date = date('h:i:s A', strtotime($today . ' + 10 hours'));
(untested)
$date = date('h:i:s A', strtotime($today . " +10 hours"));
Full code that shows now and 10 minutes added.....
$nowtime = date("Y-m-d H:i:s");
echo $nowtime;
$date = date('Y-m-d H:i:s', strtotime($nowtime . ' + 10 minute'));
echo "<br>".$date;
In order to increase or decrease time using strtotime you could use a Relative format in the first argument.
In your case to increase the current time by 10 hours:
$date = date('h:i:s A', strtotime('+10 hours'));
In case you need to apply the change to another timestamp, the second argument can be specified.
Note:
Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.
So, the recommended way since PHP 5.3:
$dt = new DateTime(); // assuming we need to add to the current time
$dt->add(new DateInterval('PT10H'));
$date = $dt->format('h:i:s A');
or using aliases:
$dt = date_create(); // assuming we need to add to the current time
date_add($dt, date_interval_create_from_date_string('10 hours'));
$date = date_format($dt, 'h:i:s A');
In all cases the default time zone will be used unless a time zone is specified.