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
61
votes
6 answers

Calculating the difference between two dates in Swift

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:…
Mischa
  • 15,816
  • 8
  • 59
  • 117
41
votes
5 answers

Get a PHP DateTime difference in days, considering midnight as a day change

What is the simplest way to get the difference in days between two PHP DateTimes, considering midnight as a day change (just like the DATEDIFF(DAY) SQL function does)? For example, between today at 13:00 and tomorrow at 12:00, I should get 1 (day),…
marcv
  • 1,874
  • 4
  • 24
  • 45
32
votes
4 answers

How we can add two date intervals in PHP

i want to add two date intervals to calculate the total duration in hours and minutes in fact i want to perform addittion as shown below: $a = new DateTime('14:25'); $b = new DateTime('17:30'); $interval1 = $a->diff($b); echo "interval 1 : " .…
Sheikh Rahat Ali
  • 1,293
  • 8
  • 37
  • 61
31
votes
7 answers

Are PHP DateInterval comparable like DateTime?

I discovered that a DateTime object in PHP can be compared to another as the ">" and "<" operators are overloaded. Is it the same with DateInterval? As I was trying to answer this question, I found something strange:
artragis
  • 3,677
  • 1
  • 18
  • 30
21
votes
2 answers

php Object of class DateInterval could not be converted to string

i've tried using date_diff and date_create to get a difference from two date that's already converted to string. here's the code: $date_1 = date_create(); $date_now = date_format($date_1, 'Y-m-d'); //echo $date_now .…
Christian Burgos
  • 1,572
  • 9
  • 26
  • 48
19
votes
3 answers

How to convert string duration to ISO 8601 duration format? (e.g. "30 minutes" to "PT30M")

There are plenty of questions asking how to do this the other way (converting from this format), but I can't find anything on how to output in the ISO 8601 duration format in PHP. So I have a heap of duration strings in human readable format - I…
Chris
  • 706
  • 1
  • 6
  • 12
16
votes
0 answers

Strange behavior DateInterval in PHP

I have this code: $i1 = DateInterval::createFromDateString('10 minutes'); $i2 = DateInterval::createFromDateString('30 minutes'); var_dump($i1 > $i2); var_dump($i1 > $i2); var_dump($i1); var_dump($i1 > $i2); var_dump($i1 > $i2); And this…
15
votes
5 answers

Regex for ISO 8601 durations

I need a regular expression to validate durations in the ISO 8601 duration format (with the exception of fractional parts which I don't need). PnYnMnDTnHnMnS PnW Here is what I have: ^P(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?$ The…
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
10
votes
2 answers

Displaying a DateInterval in Twig

I'm trying to display a DateInterval in Twig using the following code: {{ event.endTime.diff(event.startTime)|date("i's''") }} where event is an Entity who get 2 DateTime object: endTime and startTime. With that command I've got this…
homer
  • 882
  • 8
  • 23
10
votes
5 answers

php reverse order of time period in foreach

hi i have script gives me list of days date between a week. i need the reverse the the order. alredy tried rsort, reverse_array, and much more some how all giving error. thanks. script $date2 = "$lastweek2"; $date1 = "$lastweek1"; $start = new…
sarabrownD
  • 125
  • 1
  • 7
9
votes
3 answers

PHP Converting DateInterval to int

I'm using this code: $due_date = new DateTime($_POST['due_date']); $today = new DateTime(); $months = $due_date->diff($today); $months->format("%m"); $fine = 0.02 * $price * $months; // i got error in this line $bill = $price + $fine; I want to…
user6247538
8
votes
1 answer

How to create a DateInterval from a time string

If I have a time format string like "14:30:00" ("hours:minutes:seconds"), how do I get a DateInterval from the string? I can get a DateTime: $datetime= DateTime::createFromFormat("H:i:s","14:30:00"); But I need to add it to another DateTime…
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
8
votes
2 answers

Add minutes to PHP Datetime to calculate start/end of event

I would like to calculate with PHP the start and end datetime of an event. I get the start of this event and the duration to add to get the end. So I tried the following code: $startTime = $this->getStartTime(); $endTime =…
Stefano
  • 3,213
  • 9
  • 60
  • 101
7
votes
1 answer

Php Class 'DateInterval' not found

I am using php version 5.4.24 and I can't use the class DateInterval. I get this error: PHP Fatal error: Class 'DateInterval' not found $interval = new DateInterval('P91D'); What should I do in order to make this code work?
user1032317
7
votes
2 answers

Average dateInterval php

Hello i need to calculate an average time between some DateInterval. Actually i've some Dateinterval like this : for ($i = 0 ; $i < count($startDate) ; $i++) { $diffTable[] = date_diff($finishDate[$i], $startDate[$i]); echo…
Damien Locque
  • 1,810
  • 2
  • 20
  • 42
1
2 3
14 15