Questions tagged [dateinterval]

Data type used for capturing a interval between two date objects. This is found in PHP, Swift, and likely other languages too.

Data type used for capturing a interval between two date objects. This is found in many languages including PHP and Swift (and likely other languages, too).

For more information, see:

221 questions
6
votes
2 answers

php - determine whether DateTime falls on a DateInterval

I am trying to determine whether a DateTime exactly coincides with a possible occurance of a DateInterval. The intervals I'm using need to allow for 'Nth Xday of next month' type format specifications and I'm not sure such a thing is possible given…
pospi
  • 3,540
  • 3
  • 27
  • 26
5
votes
2 answers

php date interval/date period not behaving as expected

After doing some research into Dateperiod, it turns out it by default excludes the enddate, even though it doesn't explicitly state that anywhere in the PHP manual. I also didn't notice any option for including it. The only option there seems to be…
jz3
  • 521
  • 3
  • 13
  • 21
5
votes
2 answers

Is there a name for date/time interval format like "1h10m"

It's commonplace even outside of software to communicate time or date intervals in a truncated manner. For example: 1h10m translates to "One hour and ten minutes." This could be abstracted to a set of rules. For instance: A date interval is…
Cameron Hurd
  • 4,836
  • 1
  • 22
  • 31
5
votes
3 answers

Format DateInterval as ISO8601

I am currently working on a php project and need to format a DateInterval as ISO8601 (something like this): P5D This format can be used to create DateTime and DateInterval objects, but I can't figure out a way to format a DateInterval into this…
Zwirbelbart
  • 777
  • 2
  • 8
  • 19
5
votes
1 answer

PHP DateInterval not returning last day of the month

I'm using the following code: public static function getDays($day, $m, $y) { $days = new DatePeriod( new DateTime("first $day of $m $y"), DateInterval::createFromDateString('next ' . $day), new DateTime("last day of $m $y") ); …
lukeocodes
  • 1,192
  • 1
  • 16
  • 31
5
votes
1 answer

DateTime/DateInterval adding 25 hours each refresh gives a completely different result

Here is my code: $timezone = new \DateTimeZone('America/New_York'); $date1 = new \DateTime (date("Y:m:d H:i:s", time()), $timezone); $date1->add(new \DateInterval ("PT24H")); echo $date1->format('Y-m-d h:s:m'); This should add 24 hours…
user1513171
  • 1,912
  • 6
  • 32
  • 54
5
votes
4 answers

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

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…
Niki Romagnoli
  • 1,406
  • 1
  • 21
  • 26
4
votes
2 answers

What can go wrong when adding months with a DateInterval and DateTime::add?

I failed to find a proper solution to this issue. As you see in Example #3 in the PHP documentation, they state that one must beware when adding months using the DateInterval in DateTime::add. There's not really any explanation for why the method's…
Ronni Egeriis Persson
  • 2,209
  • 1
  • 21
  • 43
4
votes
1 answer

DateInterval doesn't accept ISO 8601 timeperiod with milliseconds

I have this ISO 8601 time period string: P0Y0M0DT3H5M0.000S and PHP7.4 fails to construct a DateInterval with it.
ivoba
  • 5,780
  • 5
  • 48
  • 55
4
votes
2 answers

PHP DateTime DateInterval isset changes after var_dump

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…
4
votes
7 answers

MYSQL Add working days to date

I want to add 5 days to the provided date, but the calculation must skip weekends. I already know how to add 5 days without skipping weekends: SELECT DATE_ADD(`date_field`, INTERVAL 5 DAY) As FinalDate FROM `table_name`; Now I want the returned…
Luthando Ntsekwa
  • 4,192
  • 6
  • 23
  • 52
4
votes
2 answers

PHP wrong DateTime::diff() returns wrong DateInterval

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');…
rgazelot
  • 518
  • 6
  • 14
4
votes
1 answer

PHP Mysql Calculating total flight hours per day from just two datetimes across day

Row_id Sensor_ID datetime_takeoff datetime_landing 1 SFO 2013-09-18 04:34:22 2013-09-19 08:34:22 2 BWI 2013-09-18 04:34:22 2013-09-18 16:55:23 3 BWI 2013-09-18 20:34:22 2013-09-19 10:34:22 4 …
TOO
  • 47
  • 4
4
votes
1 answer

How to get a DatePeriod with "every first thursday in month"?

I'm having two DateTime-objects: $start = new DateTime('first thursday of June 2012'); $end = new DateTime('2012-12-31'); I need a DatePeriod that contains all first Thursdays of the months between this two dates. When using $interval = new…
acme
  • 14,654
  • 7
  • 75
  • 109
3
votes
1 answer

How can I create the days property in a DateInterval extension class?

I want to extend the DateInterval class to add my own methods. For this I want to create an instance of the DateIntervalEx extension class from a DateInterval object. Example: $dateInterval = date_create('yesterday')->diff(date_create('today…
jspit
  • 7,276
  • 1
  • 9
  • 17
1
2
3
14 15