The DateTimeZone constructor only accepts a region name:
new DateTimeZone('Europe/London');
And not an offset from UTC:
new DateTimeZone('+01:00'); // Unknown or bad timezone (+01:00)
However, it is possible to obtain such a DateTimeZone from a DateTime:
(new DateTime('2012-12-28T00:00:00+01:00'))->getTimezone()->getName(); // +01:00
So this is a bit weird. Is there a way to directly obtain a DateTimeZone from an offset?
In addition to Rafał's answer, the simplest way I've found so far is:
DateTime::createFromFormat('O', '+01:00')->getTimezone();
Edit
This was a bug that has been fixed in PHP 5.5.10. It now works!
Modern answer:
new DateTimeZone('+0100');
Documentation:
http://php.net/manual/en/datetimezone.construct.php
Check out this function.
http://pl1.php.net/manual/en/function.timezone-name-from-abbr.php
You'll need to convert hours to seconds and pass them as second parameter.
Sth. like new DateTimeZone(timezone_name_from_abbr('', 3600, 0)) should work.
I dont think that there is any predefined way you wanna go. But if you declare any global function that will return you date and time with the offset added, it might help you.
Example :
function getDateTime($format="dd-mm-YY"){
$currDate= date($format);
$currDate=date($format,strtotime("+1 day",$currDate); // or whatever needed instead of +1 day
}
Related
So,
I know a lot of requests and question has been askeb about this subject but none really worked for my case... I'm working on a liscensing api with php (supposed to be easy) and I get a string date (2000-01-01) from my db and the length of the subscription. So I'm creating a DateTime Object with it using this :
$created_at = date_create($result["created_at"]);
date_add($created_at, date_interval_create_from_date_string($result["length"]." days"));
But for some unknowed reason, It seems I can't get the current date in a DateTime object so I can just compare them with <>=. Even if I use date_sub() or date_diff() It still require two DateTime object. I'm really deseperate at this point so I figured I could ask for some help.
Hope I didn't miss anything obvious
You can use the 'now' attribute,
$today = new DateTime('now'); to get the current time.
Don't forget to set your timeregion in your php.ini to be able to get the right time.
And if you want to compare them, you can use date_diff and then
$var->format('%r') to get the value.
%r is going to be empty if the result is positive.
Good luck!
Is it possible in PHP to change the DateTime() to a custom time difference. Like adding it 20 min and everytime I call new DateTime() I would get those extra 20 min to the current date.
I thought about __construct() but didn't see how to fix it.
I can use a function of course, but just curious to know if its possible to change the prototype new DateTime()
Yes, it's possible:
$date = new DateTime();
$date->modify("+20 minutes"); //or whatever value you want
Example online
Ilia Rostovtsev's answer is a good one for achieving your goal. Alternatively you can also use the DateInterval class with DateTime::add(). Personally, I prefer that one because you don't need to know how strings need to look like while DateInterval uses a standard like P1d for a day.
I wouldn't use inheritance to get a DateTime class including your wished behaviour. Instead you can create some kind of factory which contains Ilia's code (and every other code that is part for the object creation). The interval can be added as parameter. Your added interval, your 20 minutes should be stored in a constant in case you need to change that interval in the future while technically being able to use other intervals than 20 minutes.
You can do this all in one line:-
$date = (new \DateTime())->add(new \DateInterval('PT20M'));
That would be my preferred way of doing it.
See it working in PHP >= 5.3
I need to convert a specific date format into local time (Europe/Sarajevo), I have the time in this format 2013-02-17T15:00:00Z which I don't really understand and this is why I don't know how to convert it to the Europe/Sarajevo time, who knows maybe it is already Sarajevo time, I don't know...
OK I can parse it and remove the T and Z and get a time but these letters mean something, probably they affect the result time...
The result time is for example 2013-02-17 18:00:00, probably there will be a difference due to the letters T and Z which are probably time offset.
Use DateTime. It's much better for working with timezones:
$datetime = new DateTime('2013-02-17T15:00:00Z');
$datetime->setTimeZone(new DateTimeZone('Europe/Sarajevo'));
echo $datetime->format('c');
Reference
DateTime
DateTimeZone
See it in action
You can use php date function like this
$date = '2013-02-17T15:00:00Z';
echo date('Y-m-d H:i:s',strtotime($date));
See the Manual
I create an object
$date = new DateTime();
It is set to current date 2011-04-01 21:43:40. I try the following
$date->modify('midnight');
I expect the object to set to 2011-04-01 00:00:00. But nothing happened. Object hadn't beed modified, and continue to have a 2011-04-01 21:43:40 date. I just want to reset the time to midnight (00:00:oo).
http://codepad.org/w5RAF0Lh
This piece of code (with midnight) will not work without date.timezone setting
UPDATE: this piece of code requires PHP 5.3.6 to work correctly. In previous versions DateTime::modify('midnight') didn't work
Got a few questions, perhaps the will help illuminate the problem...
Is a timezone set in your php.ini file?
After you create the new DateTime() object are you using var_dump() or some other function to view its parameters and get the set date?
Have you tried and been successful passing other date and time formats into the modify method?
Doctrine checks if the DateTime object has changed its reference.
Modifying an object doesn’t change the reference, so for doctrine, this is not a change.
Use new \DateTime('midnight') instead.
I had the same problem!
However the returned date was correct, so what I did is:
#$date->modify('midnight');
Solved using
$date = new DateTime(date('Y-m-d H:i:s', strtotime('today midnight')));
echo $date->format('Y-m-d H:i:s');
I'm using Zend_Date to manage dates/times in a scheduling app.
The only way I'm able to determine the user's Timezone is via Javascript which only gives a Timezone Offset. Zend_Date only seems to take Timezones in the 'America/New_York' format.
Is there a way to get the users timezone in this format or set the Timezone Offset with Zend_Date?
Thanks!
Nicky,
You don't really need Zend_Date for this as the PHP intrinsic DateTime and DateTimeZone objects work well for this; however, I can point you in the right direction if you really need to use Zend_Date.
See the following examples
$date = new Zend_Date(1234567890, false, $locale);
$date->toString... (see: http://framework.zend.com/manual/en/zend.date.constants.html for more details)
Use the following Constants for ISO 8601:
(Z = Difference of time zone [+0100])
This should get you to where you need to be. Please post code samples if you get stuck.
You could use the timezone_name_from_abbr function:
$javascript_offset = '-1';
$zone = timezone_name_from_abbr('', $javascript_offset * 3600, 0);
You will also need to consider DST. Have a look at this example for more info.
You can use javascript to set a cookie to the users timezone offset.
Then in php you can have an array indexed by offset
$timezones = array (
'-1' => 'adfas/adsfafsd',
'-2' => 'adsfasdf/asdfasdf'
...
);
$date->setTimezone( $timezones[$_COOKIE['tz_offset']] );