dateinterval

PHP DateInterval missing a period

心已入冬 提交于 2020-01-16 06:09:52
问题 My aim is to generate a timestamp of every minute between two given dates (the actual problem is different, but this is similar in structure and simpler to describe). I'm using the following code: $Interval = DateInterval::createFromDateString('1 minute'); $Period = new DatePeriod('2013-01-01', $Interval, '2014-01-01'); foreach ($Period as $Dt){ echo "Minute" . $Dt->format('i'); } Problem is when I run this, I would expect to find 365*24*60 entries, or 525,600, but instead only 525,540 are

PHP: Weird DateInterval length calculation

一个人想着一个人 提交于 2020-01-04 11:03:50
问题 While playing with PHP, I strucked on this : <?php $FebruaryTheFirst = \DateTime::createFromFormat('Y-m-d H:i:s', '2001-02-01 00:00:00'); $MarchTheSecond = \DateTime::createFromFormat('Y-m-d H:i:s', '2001-03-01 00:00:00'); $interval = $FebruaryTheFirst->diff($MarchTheSecond); echo $interval->m.PHP_EOL; // Outputs 0. WTF? $FebruaryTheFirstbis = \DateTime::createFromFormat('Y-m-d', '2001-02-01'); $MarchTheSecondbis = \DateTime::createFromFormat('Y-m-d', '2001-03-01'); $interval2 =

PHP wrong DateTime::diff() returns wrong DateInterval

坚强是说给别人听的谎言 提交于 2020-01-03 20:05:14
问题 I have an issue with a difference of two Datetime. Here is the command line to display the DateInterval object : php -r "\$a = new Datetime('first day of 4 months ago midnight'); \$b = new Datetime('first day of 1 month ago midnight'); var_dump(\$a->diff(\$b));" And here the DateInterval output : class DateInterval#3 (15) { public $y => int(0) public $m => int(3) public $d => int(3) public $h => int(0) public $i => int(0) public $s => int(0) public $weekday => int(0) public $weekday_behavior

How can I loop through an array of dates and group by week, month or custom quarter?

别说谁变了你拦得住时间么 提交于 2020-01-03 05:31:04
问题 I am pulling out rows from a database based on date range and order by date, for example SELECT date, code FROM records WHERE personid = '133'AND date >= '2017-02-10' AND date <= '2017-06-13' AND code IN ('P','T') ORDER BY date ASC These records I like to group or order in particular ways. One way has grouped the records by consecutive dates, for this, I have the following function: function getRecordsWithIntervals(array $records, $interval_size) { $intervals = array(); foreach (array_chunk(

DateTime::diff unexpected results

余生长醉 提交于 2020-01-03 04:21:48
问题 I have the following calculation, which I expect to return 0. However it returns 1 on many systems I have access to: Ubuntu 16.04 server (wrong) php -v PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.0.22-0ubuntu0.16.04.1, Copyright (c) 1999- 2017, by Zend Technologies echo "<?php echo DateTime::createFromFormat('Y-m-d H:i:s', '2017-12-01 00:00:00')->diff(DateTime:

PHP DateTime DateInterval isset changes after var_dump

会有一股神秘感。 提交于 2020-01-02 05:34:10
问题 Any variable in this object is !isset() but if I either var_dump($interval) or print_r($interval), these variables becomes isset(). This also applies to empty()/!empty(). So in the code below $interval->i is initially !isset() but isset() after I var_dump($interval). $future = new DateTime("2018-08-24"); $now = new DateTime(); $interval = $future->diff($now); if (isset($interval->i)) { echo 'isset' . $interval->i; } else { echo 'not isset' . $interval->i; } var_dump($interval); if (isset(

Calculating the difference between two dates in Swift

六月ゝ 毕业季﹏ 提交于 2020-01-01 02:35:16
问题 I've seen many approaches how to compute the difference between two dates in terms of a particular date component, e.g. in days, hours, months etc. (see this answer on Stackoverflow): Calendar.current.dateComponents([.hour], from: fromDate, to: toDate).hour Calendar.current.dateComponents([.day], from: fromDate, to: toDate).day Calendar.current.dateComponents([.month], from: fromDate, to: toDate).month What I haven't seen is how to make calculations with the actual Date objects. Something

Is it possible to get the “interval_spec” string from DateInterval object?

别等时光非礼了梦想. 提交于 2019-12-23 10:26:33
问题 Say I have this object instance of DateInterval: $obj=new DateInterval("P1Y12D"); Now I can do few pretty things with that $obj instance, but say I want to get out that "P1Y12D" string from the object, is it straight possible without the need to override the class? I do not find a method for this, maybe you do. 回答1: Here's my version of @Yaslaw's code. It is improved according to a current PHP community and PSR requirements. I've also tried to make it more readable and straight-forward. /** *

Ebay API ISO 8601 duration format to timestamp

时间秒杀一切 提交于 2019-12-22 09:29:12
问题 Ebay API provides me time in ISO 8601. For example, it gives me item time left: P0DT0H1M4S Here is how this time is formatted: ebay duration I want to convert this time to simple Y-m-d H:i:s format. So far I've tried date("Y-m-d", strtotime("P0DT0H1M4S")); but it does not work. 回答1: That date interval string is an ISO 8601 duration specification and can be use with DateTime() and DateInterval() $date = new DateTime(); $date->add(new DateInterval('P0DT0H1M4S')); echo $date->format('Y-m-d H:i:s

DateTime::diff weired result

[亡魂溺海] 提交于 2019-12-19 04:57:17
问题 I use DateTime::diff to compare two DateTime , but the result is very weird as if abs($date1 - $date2) != abs($date2 - $date1) . $date1 = new DateTime("1980-11-21 00:00:00"); $date2 = new DateTime("1981-11-20 00:00:00"); var_dump($date1->diff($date2,true)->days); var_dump($date2->diff($date1,true)->days); var_dump($date1->diff($date2,true)->format("%Y-%m-%d %H:%i:%s")); var_dump($date2->diff($date1,true)->format("%Y-%m-%d %H:%i:%s")); Which returns: int(364) int(364) string(15) "00-11-30 00:0