note 112472 added to datetime.diff
| From: | php at jcouture dot net | Date: | Thu, 20 Jun 2013 01:46:01 +0000 |
| Subject: | note 112472 added to datetime.diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195294@lists.php.net to get a copy of this message | ||
After wrestling with datetime diff for a while it finally dawned on me the problem was both in the
formatting of the input string and the formatting of the output.
The task was to calculate the duration between two date/times.
### Calculating Duration
1. Make sure you have a valid date variable. Both of these strings are valid:
* 2013-06-19 18:25
* 06/19/13 18:25
2. Next convert the string to a date variable
~~~
$dteStart = new DateTime($strStart);
$dteEnd = new DateTime($strEnd);
~~~
3. Calculate the difference
~~~
$dteDiff = $dteStart->diff($dteEnd);
~~~
4. Format the output
~~~
# you cannot combine the output thus the following line does not work.
print($dteDiff->format("H:I:S")); # DOES NOT WORK
# Instead, you have to return each level of the interval
print($dteDiff->format("%H").':');
print($dteDiff->format("%I").':');
print($dteDiff->format("%S"));
~~~
----
Server IP: 69.147.83.199
Probable Submitter: 69.105.136.77
----
Manual Page -- http://php.net/manual/en/datetime.diff.php
Edit -- https://master.php.net/note/edit/112472
Del: integrated -- https://master.php.net/note/delete/112472/integrated
Del: useless -- https://master.php.net/note/delete/112472/useless
Del: bad code -- https://master.php.net/note/delete/112472/bad+code
Del: spam -- https://master.php.net/note/delete/112472/spam
Del: non-english -- https://master.php.net/note/delete/112472/non-english
Del: in docs -- https://master.php.net/note/delete/112472/in+docs
Del: other reasons-- https://master.php.net/note/delete/112472
Reject -- https://master.php.net/note/reject/112472
Search -- https://master.php.net/manage/user-notes.php