I use the native PHP DateTime class for adding days to dates. But when dealing with negative dates, I encountered a strange bug. Depending on the millennium added or a day or two. Example:
$date_one = date_create("-1000-12-27");
date_modify($date_one, '+1 day');
//Return DateTime Object ( [date] => -1000-12-29 00:00:00 )
$date_two = date_create("-2000-12-27");
date_modify($date_two, '+1 day');
//Return DateTime Object ( [date] => -2000-12-28 00:00:00 )
$date_three = date_create("-3000-12-27");
date_modify($date_three, '+1 day');
//Return DateTime Object ( [date] => -3000-12-29 00:00:00 )
That is, depending on the parity of the millennium issue, or December 28 or December 29. Why is this happening? What is the problem?
Related
I've got a date like : $date = DateTime::createFromFormat('D d/m', 'Mon 05/02'); but instead of 05 february the datetime returned is DateTime Object ( [date] => 2021-02-08 10:02:10.000000 [timezone_type] => 3 [timezone] => Europe/Brussels )
Answer
Corrected with the Y input and got the right result, php was using 2021 when i was constructing 2022 year
If the (wrong) day of the week is to be ignored, then an * only needs to be set in the format instead of the "D".
$date = DateTime::createFromFormat('* d/m', 'Mon 05/02');
"Mon" is ignored and the expression "05/02" is used to determine the date.
DateTime::__set_state(array(
'date' => "2021-02-05 18:28:31.000000",
'timezone_type' => 3,
'timezone' => "Europe/Berlin",
))
Because in 2021, February 5 is Friday, and February 8 is Monday.
When I print the Array I get this
[CreatedDate] => DateTime Object ( [date] => 2013-03-20 00:00:00 [timezone_type] => 3 [timezone] => America/Denver )
I'm trying to pull the month from the date, so far no luck. Been doing a combination of the below code and got a date to return but it was 12/31/1969 which is not in my database
$month = date("m",($row['CreatedDate']));
$month = date("m",($row['CreatedDate.date']));
$month = date("m",($row['date']));
Whatever produced the DateTime object already has what you need. You just need to call format() to get the month:
echo $object->format('m');
I am retrieving a date in format of 2013-09-15 08:45:00 from the database, which is set in UTC and I need to change it to another dynamic timezone (based on user)
So far I've got
$datetime = $row->field_data_field_performance_times_field_performance_times_v;
$eventDate = DateTime::createFromFormat('Y-m-d H:i:s', $datetime, new DateTimeZone($user->timezone));
$performance_time = date_format($eventDate, 'l, j F, Y, H:i');
But it doesn't change the timezone. Any ideas what's wrong? It should be +2 hours in my case.
Your input datetime is in UTC, not user's timezone. So first you must create datetime object in UTC, and then set/change timezone to user's :
$dt = new DateTime('2013-09-15 08:45:00', new DateTimeZone('UTC'));
print_r($dt);
/*
DateTime Object
(
[date] => 2013-09-15 08:45:00
[timezone_type] => 3
[timezone] => UTC
)
*/
Now you have datetime in UTC timezone. If you wish to change timezone, just call ->setTimezone() on DateTime object :
$dt->setTimezone(new DateTimeZone('Europe/Berlin'));
print_r($dt);
/*
DateTime Object
(
[date] => 2013-09-15 10:45:00
[timezone_type] => 3
[timezone] => Europe/Berlin
)
*/
p.s. because input 2013-09-15 08:45:00 is in standard datetime format, you don't need to use DateTime::createFromFormat.
After more than an hour struggling and trying I'd like to ask it here.
Trying to make something with weeks etc. in php I got from you site this:
Get all Work Days in a Week for a given date
Nice and will work for me fine.
But ... I can't get, trying and trying, the data out of this part: [date] => 2013-08-12 00:00:00
Array
(
[0] => DateTime Object
(
[date] => 2013-08-12 00:00:00
[timezone_type] => 3
[timezone] => Europe/Amsterdam
)
How to get that date out of the array ?
Please help me out, thanks in advance for the help !
Use DateTime::format()
$dateTime = new DateTime('2013-08-12 00:00:00');
echo $datetime->format('Y-m-d'); // produces 2013-08-12
$firstMondayThisWeek= new DateTime('2013-08-12');
$firstMondayThisWeek->modify('tomorrow');
$firstMondayThisWeek->modify('last Monday');
$nextFiveWeekDays = new DatePeriod(
$firstMondayThisWeek,
DateInterval::createFromDateString('+1 weekdays'),
4
);
$dateTimes = iterator_to_array($nextFiveWeekDays);
foreach ($dateTimes as $dateTime) {
echo $dateTime->format('Y-m-d H:i:s');
}
I have the following date string
$date="Sat Apr 30 2011 18:47:47 GMT+0900 (Tokyo)"
I want to convert it to UTC time
$timestamp_UNIX = strtotime($date);
echo date("Y-m-d\TH:i:s\Z",$timestamp_UNIX);
Why do I got
2011-04-30T11:47:47Z
and not
2011-04-30T09:47:47Z
The problem is that you code does not automatically echo UTC. It echos the timestamp in whatever your default timezone is set to. This is done via date_default_timezone_set() at runtime or via the configuration setting date.timezone in your php.ini.
The modern way would be to use the DateTime and the DateTimeZone classes.
$d = new DateTime('Sat Apr 30 2011 18:47:47 GMT+0900 (Tokyo)');
print_r($d);
$d->setTimezone(new DateTimeZone('UTC'));
print_r($d);
prints
DateTime Object
(
[date] => 2011-04-30 18:47:47
[timezone_type] => 1
[timezone] => +09:00
)
DateTime Object
(
[date] => 2011-04-30 09:47:47
[timezone_type] => 3
[timezone] => UTC
)
You should use gmdate() instead of date() (or you could check the DateTime and DateTimeZone classes in PHP 5.2 / 5.3)