I'm trying to create a DateTime object offset from the present with a specific timezone. It works for 'today', or an absolute value like '2017-07-16 00:00:00', but if I try an offset, like '+1 sundays' it always has a timezone of "S".
$dtz = date_default_timezone_get();//"America/Vancouver"
$now = new \DateTime('today', new \DateTimeZone($dtz));
$sunday = new \DateTime('+1 sundays', new \DateTimeZone($dtz));
$later = new \DateTime('2357-04-13', new \DateTimeZone($dtz));
$tz1 = $now->getTimezone()->getName();//"America/Vancouver"
$tz2 = $sunday->getTimezone()->getName();//"S"
$tz3 = $later->getTimezone()->getName();//"America/Vancouver"
How should I do this?
The first parameter to DateTime should be a valid date/time string. It even takes null when using timezone as second parameter.
'+1 sundays' is not under the category of valid date/time string. Check the complete list here
Below should work -
$sunday = $now->modify('+1 sundays');
echo $sunday->getTimezone()->getName();//"America/Vancouver"
Related
I am currently working on displaying a moment. I want the view to use DateTime->format(..). The value I am getting from the API is 1502462223168. However, this is displayed as 1945-5-26 16:36 instead of 2017-8-11 16:37, since the original value exceeds the PHP_MAX_INT value on the system.
Is there a way I can use the original value, maybe as a String, to create the DateTime object?
since your timestamp value is in milliseconds, divide it by 1000 and then use DateTime, like:
$date = new DateTime();
$stamp = intval(1502462223168/1000);
$date = DateTime::createFromFormat("U", $stamp)->format("Y-m-d H:i:s");
echo $date;
Use this it will be working fine for your question
$timestamp = 1465298940;
$datetimeFormat = 'Y-m-d H:i:s';
$date = new DateTime();
// If you must have use time zones
// $date = new \DateTime('now', new \DateTimeZone('Europe/Helsinki'));
$date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);
I've made a DateTime object using
$currtime = DateTime::createFromFormat("YmdHi");
Is there a way to get a string or an integer, which contains, for example, 201604041825?
$currtime->modify("+2 hours");
$newtime = $currtime->format("YmdHi");
returned
$newtime = 2147483647
This will help you
$current_date_object= new DateTime();
$current_date_object->modify("+2 hours");
$current_date_string= $current_date_object->format("Y-m-d H:i:sO");
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)
I am unable to set timezone for my DateTime objects.
Here is my code :
$dt = DateTime::createFromFormat('U',time(),new DateTimeZone('Asia/Kolkata'));
print_r($dt->getTimeZone());
Here is the output :
DateTimeZone Object
(
)
I also tried putting these lines at the top (one at a time) - without any success:
date_default_timezone_set('Asia/Calcutta');
ini_set('date.timezone', 'Asia/Calcutta');
date_default_timezone_set('Asia/Kolkata');
ini_set('date.timezone', 'Asia/Kolkata');
This is because you specified UNIX timestamp in the parameter. See what php manual says.
The timezone parameter and the current timezone are ignored when the time parameter either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
What you are trying to do can be easily done by,
$dt = new DateTime("now", new DateTimeZone('Asia/Kolkata'));
If you have a variable that contains UNIX timestamp, first create a DateTime object with it. Then set the new TimeZone.
$dt = new DateTime("#$timestamp");
$dt->setTimezone( new DateTimeZone('Asia/Kolkata'));
http://codepad.viper-7.com/topBCR
try this
<?php
$dateTimeZoneAsia = new DateTimeZone("Asia/Kolkata");
$dateTimeAsia = new DateTime("now", $dateTimeZoneAsia);
$timeOffset = $dateTimeZoneAsia->getOffset($dateTimeAsia);
var_dump($timeOffset);
?>
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...