date-math

Best way to find the months between two dates

冷暖自知 提交于 2020-02-08 19:25:05
问题 I have the need to be able to accurately find the months between two dates in python. I have a solution that works but its not very good (as in elegant) or fast. dateRange = [datetime.strptime(dateRanges[0], "%Y-%m-%d"), datetime.strptime(dateRanges[1], "%Y-%m-%d")] months = [] tmpTime = dateRange[0] oneWeek = timedelta(weeks=1) tmpTime = tmpTime.replace(day=1) dateRange[0] = tmpTime dateRange[1] = dateRange[1].replace(day=1) lastMonth = tmpTime.month months.append(tmpTime) while tmpTime <

Php, date manipulation?

柔情痞子 提交于 2019-12-29 05:25:31
问题 I am new to php. And I would like to know some of the date manipulation things in php. // Get Current date $date = date("Y-m-d"); What if I want to subtract current date and a specific date, lets say "today - 2008-06-26"? How to do date math manipulation (add, minus, multply etc) in php? If today, a subscriber subscribes on today date 2009-06-26, 1 week later I want to delete his account from my database, how do i do that? (I am using mysql) What can we do if we store the user's date in our

PHP strtotime date difference

前提是你 提交于 2019-12-25 03:39:38
问题 I want to get the age of users. I tried: $dStart = strtotime('1985-10-24'); $dEnd = strtotime('2014-07-30'); $dDiff = $dEnd - $dStart; echo date('Y',$dDiff); But get 1998 instead 28, what would be the right code? 回答1: strtotime() is not made for date math. DateTime() is much better suited for this: $dStart = new DateTime('1985-10-24'); $dEnd = new DateTime('2014-07-30'); $dDiff = $dStart->diff($dEnd); echo $dDiff->y; Demo 来源: https://stackoverflow.com/questions/25026602/php-strtotime-date

get time difference in hours minutes and seconds

南笙酒味 提交于 2019-12-25 02:26:33
问题 I have two dates, $expiry_time = strtotime('comming from database'); $current_date = strtotime(date('Y-m-d H:i:s')); $time_diff = ($expiry_time-$current_date)/(3600); Here, $time_diff will give me difference in hours. eg. 5.67. But, I want it in hours, minute and second format. How can I achieve this? Thanks. 回答1: Use DateTime() with DateInterval() $expiry_time = new DateTime($row['fromdb']); $current_date = new DateTime(); $diff = $expiry_time->diff($current_time); echo $diff->format('%H:%I:

How to calculate the difference between two days as a formatted string?

大兔子大兔子 提交于 2019-12-23 10:27:06
问题 Here's what I've got so far: /** * Parse a duration between 2 date/times in seconds * and to convert that duration into a formatted string * * @param integer $time_start start time in seconds * @param integer $time_end end time in seconds * @param string $format like the php strftime formatting uses %y %m %w %d %h or %i. * @param boolean $chop chop off sections that have 0 values */ public static function FormatDateDiff($time_start = 0, $time_end = 0, $format = "%s", $chop = false) { if($time

How do I do date math in a bash script on OS X Leopard?

旧时模样 提交于 2019-12-20 11:22:26
问题 I realize I could whip up a little C or Ruby program to do this, but I want my script to have as few dependencies as possible. Given that caveat , how does one do date math in a bash script on OS X? I've seen a post (on another site) where someone did the following: date -d "-1 day" But this does not seem to work on OS X. Addendum: Several people have commented and responded that Ruby, Python, Perl, and the like come standard with OS X. I'm familiar with all three of these languages and could

C# Method to sum hh:mm data ???

夙愿已清 提交于 2019-12-19 19:54:33
问题 I want a method that will sum data that is string in the format hh:mm (time hours and minutes) 0:15 + 0:15 = 0:30 回答1: Convert the strings to TimeSpan s and then call the .Add method. TimeSpan s1 = TimeSpan.Parse("0:15"); TimeSpan s2 = TimeSpan.Parse("0:45"); TimeSpan s3 = s1 + s2; // not tested; should work. Ref: http://msdn.microsoft.com/en-us/library/system.timespan.aspx 来源: https://stackoverflow.com/questions/2871557/c-sharp-method-to-sum-hhmm-data

Simple way to handle time in C#?

有些话、适合烂在心里 提交于 2019-12-18 05:03:29
问题 My current approach is something like DateTime startHour = new DateTime(1900,1,1,12,25,43); DateTime endHour = new DateTime(1900,1,1,13,45,32); // I need to, say, know if a complete DateTime instance // is later than startHour plus 15 minutes DateTime now = DateTime.Now(); startHour = startHour.addMinutes(15); if (now.CompareTo(new DateTime(now.Year, now.Month, now.Day, startHour.Hour, startHour.Minute, startHour.Second)) > 0) { //I can do something now } This is very cumbersome and even

Simple way to handle time in C#?

三世轮回 提交于 2019-12-18 05:03:05
问题 My current approach is something like DateTime startHour = new DateTime(1900,1,1,12,25,43); DateTime endHour = new DateTime(1900,1,1,13,45,32); // I need to, say, know if a complete DateTime instance // is later than startHour plus 15 minutes DateTime now = DateTime.Now(); startHour = startHour.addMinutes(15); if (now.CompareTo(new DateTime(now.Year, now.Month, now.Day, startHour.Hour, startHour.Minute, startHour.Second)) > 0) { //I can do something now } This is very cumbersome and even

How can i parse a date to same day of previous month?

浪尽此生 提交于 2019-12-14 03:59:05
问题 How can i parse a date to same day of previous month in PHP? Example: input Output 2018-07-25 -> 2018-06-25 2018-07-31 -> 2018-06-30 (because there is no 31) My code $d = "2018-07-25"; $xd = date_parse_from_format("y-m-d", $d last month); $output_d = date_create($xd)->format('Y-m-d'); 回答1: <?php $date = new DateTimeImmutable("2018-07-31"); $previous = $date->sub(new DateInterval('P1M')); $lastMonth= $date->modify('last day of previous month'); if ($previous > $lastMonth) { $previous =