dateinterval

How to create a DateInterval from a time string

不打扰是莪最后的温柔 提交于 2019-12-01 17:11:44
问题 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 object, and date_add needs a DateInterval. 回答1: If you want an interval that is 14 hours and 30 minutes, simply use the constructor... $interval = new DateInterval('PT14H30M'); To break it down... P - all interval spec strings must start with P (for Period

Format DateInterval as ISO8601

你说的曾经没有我的故事 提交于 2019-12-01 04:33:52
问题 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 format. Is there any? If not, what might be a lightweight solution to that? 回答1: Well, if you look at the spec for the format when you construct one: Y years M months D days W weeks. These get converted into days, so can not be combined with D. H hours

Regex for ISO 8601 durations

被刻印的时光 ゝ 提交于 2019-11-30 13:28:20
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 only problem is that the strings P and PT are allowed with this regex as all of the parts are "zero or one" ? . There needs to be at least one component (date or time) If there is a T then there needs to be a time component (H, M, or S) If there is a T then there may or may not be any date components (Y, M, or D) Overflow is allowed (e.g. P72H is mostly

PHP: How to convert string duration to ISO 8601 duration format? (ie. “30 minutes” to “PT30M”)

霸气de小男生 提交于 2019-11-30 13:12:31
There are plenty of questions asking how to do this the other way (converting from this format), but I cant 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 want to convert them into the ISO 8601 format on the fly to print the durations for HTML5 microdata. Below is a sample of some of the strings coming in, and how they should be formatted "1 hour 30 minutes" --> PT1H30M "5 minutes" --> PT5M "2 hours" --> PT2H You get the idea. I can push the string into an interval object in PHP: date_interval_create_from

Regex for ISO 8601 durations

随声附和 提交于 2019-11-29 19:11:54
问题 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 only problem is that the strings P and PT are allowed with this regex as all of the parts are "zero or one" ? . There needs to be at least one component (date or time) If there is a T then there needs to be a time component (H, M, or S) If there is a T

PHP: How to convert string duration to ISO 8601 duration format? (ie. “30 minutes” to “PT30M”)

亡梦爱人 提交于 2019-11-29 18:06:43
问题 There are plenty of questions asking how to do this the other way (converting from this format), but I cant 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 want to convert them into the ISO 8601 format on the fly to print the durations for HTML5 microdata. Below is a sample of some of the strings coming in, and how they should be formatted "1 hour 30 minutes" --> PT1H30M "5 minutes" --> PT5M "2 hours" -->

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

旧巷老猫 提交于 2019-11-28 02:03:33
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 behavior is as such and what I can do to avoid this, which I find to be an error at first sight. Anyone have some insight into this? The issue is that each month can have a different number of days in them. The question is what you're doing when you want to increment a date by 1 month. Per the PHP documentation if you're on January 31st (or 30th)

Are PHP DateInterval comparable like DateTime?

心已入冬 提交于 2019-11-27 14:35:55
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: <?php $today = new DateTime(); $release = new DateTime('14-02-2012'); $building_time = new DateInterval('P15D'); var_dump($today->diff($release)); var_dump($building_time); var_dump($today->diff($release)>$building_time); var_dump($today->diff($release)<$building_time); if($today->diff($release) < $building_time){ echo 'oK'; }else{ echo 'Just a test'; } It always echoes "Just a

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

大兔子大兔子 提交于 2019-11-26 23:37:16
问题 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 behavior is as such and what I can do to avoid this, which I find to be an error at first sight. Anyone have some insight into this? 回答1: The issue is that each month can have a different number of days in them. The question is what you're doing

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

匆匆过客 提交于 2019-11-26 21:57:29
问题 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 represented as a combination of _h , _m , (and so on), where _ characters represent non-negative integers or floats, which are summed into one date interval object. Mixing days, hours, minutes are allowed. For example, 0.5d1h60m would be a synonym for 14h