Creating timestamp for 5/13/2014 # 3pm - php

I have an application that needs to send a UTC timestamp in order for it to work correctly. In my application a user can have any number of timezones. So if they pick 3pm and their timezone is America/New_York, it is a different 3pm than if it was America/Chicago.
I need to figure out a way to change the date into the right UTC timestamp. I know I can use date_default_timezone_set("UTC")...but I don't think will work correctly.
I think I need to calculate a difference between UTC and regular timezone, but I am not sure. Any advice is welcomes.
date_default_timezone_set("UTC");
echo strtotime('5/13/2014 3:00 PM');
1399993200
date_default_timezone_set("America/New_York");
echo strtotime('5/13/2014 3:00 PM');
1400007600
As you can tell these 2 values are different.
EDIT: Here is what my code looks like. It doesn't seem to work correctly as the application doesn't show the event in the right time.
$previous_timezone = date_default_timezone_get();
date_default_timezone_set("UTC");
$aceroute_schedule = $this->sale_lib->get_send_to_aceroute_schedule();
if (($start_time = strtotime($aceroute_schedule['aceroute_schedule_date']. ' '.$aceroute_schedule['aceroute_schedule_time_start'])) !== FALSE)
{
//Append 000 as as string for 32 bit systems
$start_epoch = $start_time.'000';
$end_epoch = strtotime('+ '.$aceroute_schedule['aceroute_duration'].' minutes', $start_time).'000';
}
else //Default to current time + 1 hour
{
//Append 000 as as string for 32 bit systems
$start_epoch = time().'000';
$end_epoch = strtotime('+1 hour', time()).'000';
}
$event->start_epoch = $start_epoch;
$event->end_epoch = $end_epoch;

Update:
This will now create a DateTime object in the user's DateTimeZone ('America/New_York'). And then it will set that object's timezone to UTC. To get the timestamp (or other string representations of date), use ::format().
# Create NY date
$NY = new DateTimeZone("America/New_York");
$NYdate = new DateTime('5/13/2014 3:00 PM', $NY);
# Set timezone to UTC
$UTC = new DateTimeZone("UTC");
$UTCdate = $NYdate->setTimezone($UTC);
# Get timestamp (PHP 5.2 compatible)
$timezone = $UTCdate->format('U');
var_dump($timezone); // a string containing UNIX timestamp
First I create 2 DateTime objects based off of their respective DateTimeZone objects. Then we can either use OOP ::diff() to get another object containing information about the time difference. Or we can use simple integers representing the difference in seconds from ::getTimestamp.
$date = '5/13/2014 3:00 PM';
# Create NY date
$NY = new DateTimeZone("America/New_York");
$NYdate = new DateTime($date, $NY);
# Create UTC date
$UTC = new DateTimeZone("UTC");
$UTCdate = new DateTime($date, $UTC);
# Find difference object
$diff = $NYdate->diff($UTCdate);
var_dump($diff); // a DateInterval object containing time difference info
# Find difference in seconds
$diff = $NYdate->getTimestamp() - $UTCdate->getTimestamp();
var_dump($diff); // an integer containing time difference in seconds
Links:
DateTimeZone
DateTime
DateInterval

Example in http://www.php.net/manual/en/datetime.settimezone.php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
The first line creates a DateTIme object, using the timezone Pacific/Nauru.
You can then change the timezone using setTimezone as shown in line 4, and the output will be modified accordingly.
note: the default timezone (if you don't specify it in the 2nd parameter in line 1) is the one set in your php.ini file, which you can modify (at runtime) with date_default_timezone_set("America/New_York")
note2: the 1st parameter in line 1, is equivalent to the 1st parameter of the strtotime function.
note3: the format method takes the same format parameter as date (http://www.php.net/manual/en/function.date.php)

Related

Is it safe to convert ISO datetime with strtotime

For example
strtotime("2018-12-06T09:04:55");
strtotime("2021-07-09T14:09:47.529751-04:00");
I read in the php manual that ISO dates should be avoided when using strtotime, why ?
Should I extract date time from the string before using strtotime.
strtotime() will convert a string WITHOUT a timezone indication as if the string is a time in the default timezone ( date_default_timezone_set() ). So converting a UTC time like '2018-12-06T09:04:55' with strtotime() actually yields a wrong result. In this case use:
<?php
function UTCdatestringToTime($utcdatestring)
{
$tz = date_default_timezone_get();
date_default_timezone_set('UTC');
$result = strtotime($utcdatestring);
date_default_timezone_set($tz);
return $result;
}
?>
If the date string contains a time zone, strtotime also takes this into account.
$strDate = "2018-12-06T09:04:55 UTC";
$ts = strtotime($strDate); // int(1544087095)
If the time zone is missing in the date string, the default time zone is used. My Timezone is "Europe/Berlin".
$strDate = "2018-12-06T09:04:55";
$ts = strtotime($strDate); // int(1544083495)
As a result, we get a different timestamp.
If I want to convert a date string from another time zone into a time stamp, then the best solution is to do it with the DateTime object. There I can enter the correct time zone in the 2nd parameter when creating the object.
$strDate = "2018-12-06T09:04:55";
$dt = new DateTime($strDate, new DateTimeZone('UTC'));
$ts = $dt->getTimeStamp(); // int(1544087095)
Important: If the date string contains a valid time zone, this has priority over the 2nd parameter.
$strDate = "2018-12-06T09:04:55 UTC";
$dt = new DateTime($strDate, new DateTimeZone('Europe/Berlin'));
/*
DateTime::__set_state(array(
'date' => "2018-12-06 09:04:55.000000",
'timezone_type' => 3,
'timezone' => "UTC",
))
*/
The DateTimeZone ('Europe/Berlin') is ignored here.
Since strtotime also accepts a time zone in the date string, the time zone can also be added with a string concatenation.
$strDate = "2018-12-06T09:04:55";
$ts = strtotime($strDate." UTC"); //int(1544087095)
The UTCdatestringToTime function does the same. However, it is not nice to temporarily change the default time zone in the PHP script.

How to convert UTC timestamp Value to local date time

I have a UTC timestamp value 1615958170523 and I want to convert it into our local timezone.
I have tried this method:
The timestamp is in milliseconds that's why firstly I have converted in seconds and then used the below method.
$Date = date('m-d-Y H:i:s', 1615958170523/1000);
It always returns the time ~6hours ago i.e 03-17-2021 05:16:10 (Considering current time here), I don't want to add +5:30 hours to do the same.
Is it possible that we can use a standard method that means in-built functions which may be provided by Cakephp or PHP so that I can get the answer for the same?
I have also tried this one:
$gmtTimezone = new \DateTimeZone('GMT');
$myDateTime = new \DateTime(1615958170523/1000, $gmtTimezone);
It returns the same as I have used the date function.
You need to change the timezone after to define the timestamp in GMT.
$timestamp = 1615958170523/1000;
$myDateTime = \DateTime::createFromFormat('U', (int)$timestamp);
echo $myDateTime->format('Y-m-d H:i:s'), PHP_EOL; // 2021-03-17 05:16:10
$myDateTime->setTimezone(new \DateTimeZone('Europe/Paris'));
echo $myDateTime->format('Y-m-d H:i:s'), PHP_EOL; // 2021-03-17 06:16:10
$myDateTime->setTimezone(new \DateTimeZone('America/Denver'));
echo $myDateTime->format('Y-m-d H:i:s'), PHP_EOL; // 2021-03-16 23:16:10
See DateTime::setTimezone() documentation
You should use the FrozenTime which will use the default Timezone you set in your config/app.php

Convert date into timestamp where strtotime has already been used

I' am trying to convert the date of next 7 days into timestamp so that I can compare against my date timestamp in database to get some results.
This function is used to get the next 7 days from today
$next_date = date("d/m/Y", strtotime("7 day"))
Output
30/04/2014
Now I' am again running strtotime() on $next_date variable who holds the next 7days and converting to timestamp.
echo strtotime($next_date);
This is not working. I followed this stackoverflow answer and few others.
As an alternative suggestion you could look at PHP's internal DateTime() and DateInterval() classes. It makes it a bit easier to convert between formats and do date/time addition and subtraction imho. DateInterval requires at least PHP version 5.3.
An example:
// create a current DateTime
$currDate = new DateTime();
// copy the current DateTime and
// add an interval of 7 days
$nextDate = clone $currDate;
$nextDate->add(new DateInterval('P7D'));
// both objects are easily converted to timestamps
echo $currDate->getTimestamp(); // e.g: 1398296728
echo $nextDate->getTimestamp(); // e.g: 1398901528
// and both can be easily formatted in other formats
echo $currDate->format('d/m/Y'); // e.g: 24/04/2014
echo $nextDate->format('d/m/Y'); // e.g: 01/05/2014
EDIT
For completeness, here's another example of how you can add seven days to a DateTime object:
$now = new DateTimeImmutable();
$then = $now->modify('+7 days');
var_dump($now->format('Y-m-d'), $then->format('Y-m-d'));
Yields:
string(10) "2016-05-24"
string(10) "2016-05-31"
You can also use DateTime - the difference in this use case is that DateTime::modify() will modify the instance $now where DateTimeImmutable::modify() will return a new DateTimeImmutable object - so if you need to create a new object whilst retaining the old one, it's probably the most succinct approach.
Hope this helps :)
http://www.php.net/manual/en/datetime.construct.php
http://www.php.net/manual/en/dateinterval.construct.php
Just store the value from strtotime first?
$timestamp_in_7_days = strtotime('7 day');
$next_date = date('d/m/Y', $timestamp_in_7_days);
There is no need to throw the time back and forth between unix timestamp and date-format.

DateTime class if not displaying the correct data

I am using DateTime class for the first time to convert between different time zones.
Here is what I have
$USER_TIME_ZONE = new DateTimeZone('America/Los_Angeles');
$UTC = new DateTimeZone('UTC');
$schedule_date = new DateTime($call['triggerOn'], $USER_TIME_ZONE);
echo $schedule_date->format('m/d/Y h:i A');
$schedule_date = new DateTime($call['triggerOn'], $UTC);
echo $schedule_date->format('m/d/Y h:i A');
Here is how I am going through my result and trying to convert them
foreach ( $activities AS $call){
$USER_TIME_ZONE = new DateTimeZone('America/Los_Angeles');
$UTC = new DateTimeZone('UTC');
$schedule_date = new DateTime($call['triggerOn'], $UTC);
echo $schedule_date->format('m/d/Y h:i A');
}
The following are value for $call['triggerOn']
2013-02-27 18:00:37
2013-02-02 01:11:07
2013-01-10 17:12:14
2013-02-27 22:29:42
2013-02-27 22:28:38
2013-02-25 21:53:12
2013-02-14 14:35:48
2012-12-13 14:03:16
2013-03-04 19:04:20
2013-03-01 18:52:48
2013-03-05 15:46:56
2013-03-11 15:26:17
2013-02-07 18:17:30
2013-03-05 18:04:25
Both of my outputs are the same! I don't understand why. Is there a configuration that I need to do on the server side as I have PHP running on Windows Server 2008 R2.
Thank you for your help and time.
The DateTime::format() method is returning the time in the timezone the data was created in. There's no conversion going on. Thus your output times are going to be the same as your input times regardless of the timezone you pass in. You can verify this by adding an 'e' to the format parameter. You will see that in the first case the timezone is America/Los_Angeles and in the second it is UTC.
You're probably trying to convert the time between timezones, right? In order to do that just create a single new DateTime object in one timezone, call the setTimezone method with the second timezone, and then format the result.
All of this assumes that the $call['triggerOn'] value is neither a timestamp nor a value with the timezone identified. In that case the second parameter of the DateTime constructor is ignored.
Knowing the value of $call['triggerOn'] would help, but would this work:
$USER_TIME_ZONE = 'America/Los_Angeles';
$UTC = 'UTC';
$schedule_date = new DateTime( $call['triggerOn'], $USER_TIME_ZONE );
echo $schedule_date->format( 'm/d/Y h:i A' );
$schedule_date = new DateTime( $call['triggerOn'], $UTC );
echo $schedule_date->format( 'm/d/Y h:i A' );
Basically, instead of creating new DateTime objects to use as parameters for other new DateTime objects, what if you just use the timezone string instead? Does that work?

php check if specified time has expired

I am trying to compare the current datetime, with a datetime from the database using string, as the following:
$today = new DateTime("now");
$todayString = $today->format('Y-m-d H:i:s');
if($todayString >= $rows["PrioritizationDueDate"])
{...}
$todayString keeps giving me the time 7 hours earlier (i.e now its 11:03pm, its giving me 16:04).
More, is it better to compare this way, or should i compare using datetime objects?
$todayString keeps giving me the time 7 hours earlier
you have to setup a timezone for the DateTime object I believe.
is it better to compare this way
I doubt so.
The general way is to compare in the query, using SQL to do all date calculations and return only matching rows.
Set a correct timezone in the constructor to DateTime.
$today = new DateTime("now", new DateTimeZone('TimezoneString'));
Where TimezoneString is a valid timezone string.
Edit: For a more complete example using DateTime objects, I would use DateTime::diff in conjunction with DateTime::createFromFormat.
$rows["PrioritizationDueDate"] = '2011-11-20 10:30:00';
$today = new DateTime("now", new DateTimeZone('America/New_York'));
$row_date = DateTime::createFromFormat( 'Y-m-d H:i:s', $rows["PrioritizationDueDate"], new DateTimeZone('America/New_York'));
if( $row_date->diff( $today)->format('%a') > 1)
{
echo 'The row timestamp is more than one day in the past from now.';
}
Demo
First set time zone using this function
date_default_timezone_set('UTC');
Then either you can use function strtotime() or get difference directly...

Categories