dateinterval

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

ⅰ亾dé卋堺 提交于 2019-12-17 18:47:53
问题 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), even though the interval is less than 24 hours. $date1 = new DateTime("2013-08-07 13:00:00"); $date2 = new DateTime("2013-08-08 12:00:00"); echo $date1->diff($date2)->days; // 0 回答1: You could ignore the time portion of the date string $date1 =

Class 'DateInterval' not found

↘锁芯ラ 提交于 2019-12-14 03:26:32
问题 I want to get the date between two dates excluding last date, following code i will used to find dates. But it trigger an error saying: Class 'DateInterval' not found Code: $start = new DateTime('2014-08-06'); $end = new DateTime('2014-09-06'); $oneday = new DateInterval("P1D"); $days = array(); $data = "7.5"; foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day) { $day_num = $day->format("N"); if($day_num < 6) { $days[$day->format("Y-m-d")] = $data; } } print_r($days); 回答1:

Calculate months between two dates using DateInterval without wrapping within a year

柔情痞子 提交于 2019-12-13 15:40:12
问题 I am aware this topic is pretty exhausted, but obviously not quite enough! $temp_d1 = new DateTime(date('Y-m-d', $fromTime)); // 2012-01-01 $temp_d2 = new DateTime(date('Y-m-d', $endTime)); // 2013-02-01 $interval = $temp_d2->diff($temp_d1); $monthsAhead = $interval->format('%m'); // returns 1, but I am expecting 13 How do you calculate the number of months between two dates without wrapping within a 12 month scale? 回答1: I was confusing exactly what: $monthsAhead = $interval->format('%m');

Finding Overlapping Date Intervals in Python when Intervals can Start and End on Seconds or Milliseconds

*爱你&永不变心* 提交于 2019-12-11 12:14:09
问题 What is the best approach to query a list of date intervals for overlapping intervals, given granularity to the second (or even millisecond)? From the exhaustive list of other questions regarding overlapping integer intervals, I was pointed to the Interval Tree. After reading it, I modified this implementation, which handles integer intervals, for dates. His search implementation loops from the starting search interval to the ending search interval, recursively appending from the left node or

ISO 8601 Define repeating interval on Thanksgiving?

霸气de小男生 提交于 2019-12-08 02:55:52
问题 I'm trying to define an interval that repeats every year, starting on the 4th Thursday of November at 13:30z and ending on the same day at 15:00z. Can this be done using ISO 8601? 回答1: This kind of recurrence can't be expressed with current ISO 8601. Next version of ISO 8601 will most likely include a recurring time interval format based on iCalendar (RFC 5545:2009). 来源: https://stackoverflow.com/questions/46229717/iso-8601-define-repeating-interval-on-thanksgiving

PHP DateInterval not returning last day of the month

倖福魔咒の 提交于 2019-12-07 07:17:57
问题 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") ); foreach ($days as $day) { $dates[] = $day->format("Y-m-d"); } return $dates; } It work's for the most part. I give it a range and it returns the date of each day I am after inside the range. Usage: foreach(General::getDays('friday',$this->_uri[2],$this->_uri[3]) as

Ebay API ISO 8601 duration format to timestamp

随声附和 提交于 2019-12-06 01:51:00
Ebay API provides me time in ISO 8601. For example, it gives me item time left: P0DT0H1M4S Here is how this time is formatted: ebay duration I want to convert this time to simple Y-m-d H:i:s format. So far I've tried date("Y-m-d", strtotime("P0DT0H1M4S")); but it does not work. That date interval string is an ISO 8601 duration specification and can be use with DateTime() and DateInterval() $date = new DateTime(); $date->add(new DateInterval('P0DT0H1M4S')); echo $date->format('Y-m-d H:i:s'); or as a one-liner (PHP 5.4+) echo (new DateTime())->add(new DateInterval('P0DT0H1M4S'))->format('Y-m-d H

PHP DateInterval not returning last day of the month

折月煮酒 提交于 2019-12-05 12:33:47
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") ); foreach ($days as $day) { $dates[] = $day->format("Y-m-d"); } return $dates; } It work's for the most part. I give it a range and it returns the date of each day I am after inside the range. Usage: foreach(General::getDays('friday',$this->_uri[2],$this->_uri[3]) as $date) { var_dump($date); } In July 2013, this returns the correct 4 results, all friday's in July 2013:

MYSQL Add working days to date

天涯浪子 提交于 2019-12-05 07:21:57
问题 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 value to skip weekends. Currently if date_field = 2016-07-22 the results will be 2016-07-27 But I want the results to be 2016-07-29 回答1: Try this: SELECT DATE_ADD( date_field, INTERVAL 5 + IF( (WEEK(date_field) <> WEEK(DATE_ADD(date_field, INTERVAL 5

How to benchmark code to see which runs faster

大城市里の小女人 提交于 2019-12-02 23:51:14
问题 $datetime = new DateTime('2013-01-29'); $datetime->modify('+1 day'); echo $datetime->format('Y-m-d H:i:s'); $datetime = new DateTime('2013-01-29'); $datetime->add(new DateInterval('P1D')); echo $datetime->format('Y-m-d H:i:s'); Can anyone tell me which is faster, recommended and takes less memory in huge operations in php file. I think its DateInterval PID. Any experienced developer? 回答1: To benchmark code, you can use microtime() http://php.net/manual/en/function.microtime.php <?php echo