Parse and create ISO 8601 Date and time intervals, like PT15M in PHP

前端 未结 5 467
無奈伤痛
無奈伤痛 2020-12-29 21:23

A library and webservice I am using communicates time-intervals in ISO 8601 format: PnYnMnDTnHnMnS. I want to convert such formats to seconds. And vice versa. Seconds are a

5条回答
  •  爱一瞬间的悲伤
    2020-12-29 21:36

    What you are looking for is DateTime::diff

    The DateInterval object representing the difference between the two dates or FALSE on failure as illustrated below:

    $datetime1 = new DateTime('2009-10-11');
    $datetime2 = new DateTime('2009-10-13');
    $interval = $datetime1->diff($datetime2);
    echo $interval->format('%R%d days');
    

    Just use seconds instead of dates.

    This is from http://www.php.net/manual/en/datetime.diff.php

    For a reference to DateInterval see http://www.php.net/manual/en/class.dateinterval.php

提交回复
热议问题