PHP Date Difference After 30 Days Error? - php
I have a date difference that works in minutes .. then after 1 hour goes to hours .. then after 24 hours goes to days .. then weirdly after 30 days it goes back to hours
For why?! What am I missing in this code or is this some bug?!
$d_start = new DateTime('now');
$d_end = new DateTime($last_gdate);
$d_dif = $d_start->diff($d_end);
$d_d = $d_dif->d;
$d_h = $d_dif->h;
$d_m = $d_dif->i;
if ($d_d < 1) {
if ($d_h < 1) {
$d_since = $d_m . "m";
} else {
$d_since = $d_h . "h";
}
} else {
$d_since = $d_d . "d";
}
You will notice that DateTime's diff() will restart d in the next month. You should use days if you are not going to check/honor m.
Code: (Demo)
$last_gdate = "2018-07-23 00:01:02";
$d_start = new DateTime('now');
$d_end = new DateTime($last_gdate);
$d_dif = $d_start->diff($d_end);
var_export($d_dif);
$d_d = $d_dif->days;
$d_h = $d_dif->h;
$d_m = $d_dif->i;
if ($d_d < 1) {
if ($d_h < 1) {
$d_since = $d_m . "m";
} else {
$d_since = $d_h . "h";
}
} else {
$d_since = $d_d . "d";
}
echo "\n$d_since";
Output:
DateInterval::__set_state(array(
'y' => 0,
'm' => 1,
'd' => 0,
'h' => 6,
'i' => 42,
's' => 2,
'f' => 0.002468,
'weekday' => 0,
'weekday_behavior' => 0,
'first_last_day_of' => 0,
'invert' => 1,
'days' => 31,
'special_type' => 0,
'special_amount' => 0,
'have_weekday_relative' => 0,
'have_special_relative' => 0,
))
31d
Related
How to get every month number and year between two month/year points
I am creating a function where you can give a starting year, month and end year, month and the function will print every month and year in between the two given points. I've already created a function that works perfectly but I believe this is not a good practice and there might be better way to do this. Now I am seeking your help to find a better way. P.S. I can't get full date as input. Only can get month number and year. Here is my code - function get_all_months($monthstart = null, $yearstart = null, $monthend = null, $yearend = null) { if (($monthstart === null) || ($yearstart === null) || ($monthend === null) || ($yearend === null)) { $monthend = date('m'); $yearend = date('Y'); if($monthend < 6) { $yearstart = $yearend - 1; $monthstart = (($monthend - 5) + 12); } else { $yearstart = $yearend; $monthstart = $monthend - 5; } } $month_array = array(); if ($yearstart > $yearend) { for ($m=$monthend; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $yearend); for ($y=$yearend+1; $y<$yearstart; $y++) for ($m=1; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $y); for ($m=1; $m<=$monthstart; $m++) $month_array[] = array('month' => $m, 'year' => $yearstart); } elseif ($yearend > $yearstart) { for ($m=$monthstart; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $yearstart); for ($y=$yearstart+1; $y<$yearend; $y++) for ($m=1; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $y); for ($m=1; $m<=$monthend; $m++) $month_array[] = array('month' => $m, 'year' => $yearend); } else { for ($m=$monthstart; $m<=$monthend; $m++) $month_array[] = array('month' => $m, 'year' => $yearstart); } return $month_array; } EDIT: Based on Nigel Ren's answer, this is the best way I could think of - function get_all_months($monthstart = null, $yearstart = null, $monthend = null, $yearend = null) { if (($monthstart === null) || ($yearstart === null) || ($monthend === null) || ($yearend === null)) { $monthend = date('m'); $yearend = date('Y'); if($monthend < 6) { $yearstart = $yearend - 1; $monthstart = (($monthend - 5) + 12); } else { $yearstart = $yearend; $monthstart = $monthend - 5; } } $output = []; if ($yearstart > $yearend) { $time = strtotime($yearend."-".$monthend); $last = date('m-Y', strtotime($yearstart."-".$monthstart)); } else { $time = strtotime($yearstart."-".$monthstart); $last = date('m-Y', strtotime($yearend."-".$monthend)); } do { $cur_month_year = date('m-Y', $time); $month = date('m', $time); $year = date('Y', $time); $output[] = array('month'=>$month,'year'=>$year); $time = strtotime('+1 month', $time); } while ($cur_month_year != $last); return $output; }
You can use PHP's DateInterval class which is used for these purposes. Try this code <?php function getMonthsFromRange($start, $end, $format = 'M Y') { $array = array(); // Since you wanted 1 month it is Period = 1 Month $interval = new DateInterval('P1M'); $realEnd = new DateTime($end); $realEnd->add($interval); $period = new DatePeriod(new DateTime($start), $interval, $realEnd); // Use loop to store date into array foreach ($period as $date) $array[] = $date->format($format); // Return the array elements return $array; } // Function call with passing the start date and end date $months = getMonthsFromRange('2010-10', '2011-11'); print_r($months); It's output is: Array ( [0] => Oct 2010 [1] => Nov 2010 [2] => Dec 2010 [3] => Jan 2011 [4] => Feb 2011 [5] => Mar 2011 [6] => Apr 2011 [7] => May 2011 [8] => Jun 2011 [9] => Jul 2011 [10] => Aug 2011 [11] => Sep 2011 [12] => Oct 2011 [13] => Nov 2011 )
Based on the answer here, it can be done simply by adding 1 month to the start date till you get to the end date... function get_all_months($monthstart = null, $yearstart = null, $monthend = null, $yearend = null) { $output = []; $time = strtotime($yearstart."-".$monthstart); $last = date('m-Y', strtotime($yearend."-".$monthend)); do { $month = date('m-Y', $time); $output[] = $month; $time = strtotime('+1 month', $time); } while ($month != $last); return $output; } So print_r(get_all_months(4,2008,2,2010)); gives... Array ( [0] => 04-2008 [1] => 05-2008 [2] => 06-2008 [3] => 07-2008 [4] => 08-2008 [5] => 09-2008 [6] => 10-2008 [7] => 11-2008 [8] => 12-2008 [9] => 01-2009 [10] => 02-2009 [11] => 03-2009 [12] => 04-2009 [13] => 05-2009 [14] => 06-2009 [15] => 07-2009 [16] => 08-2009 [17] => 09-2009 [18] => 10-2009 [19] => 11-2009 [20] => 12-2009 [21] => 01-2010 [22] => 02-2010 )
probably you're looking for something like this: function get_all_months($monthstart = null, $yearstart = null, $monthend = null, $yearend = null) { $month_array = []; if ($yearstart == $yearend) for ($m=$monthstart; $m<=$monthend; $m++) $month_array[] = array('month' => $m, 'year' => $yearstart); else { for ($m=$monthstart; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $yearstart); for ($y=$yearstart+1; $y<$yearend; $y++) for ($m=1; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $y); for ($m=1; $m<=$monthend; $m++) $month_array[] = array('month' => $m, 'year' => $yearend); } return $month_array; }
If I understand correctly - you get the month as a number and the year as a number e.g 6 and 1997. If we assume that the start year is always less than the end year I would suggest going like this. function distanceBetweenDates(int $sm, int $sy, int $em, int $ey) { $monthsBetweenYears = ($ey - $sy + 1) * 12; $distanceBetweenMonths = $monthsBetweenYears - $sm - (12 - $em); $startMonth = $sm + 1; $startYear = $sy; while ($distanceBetweenMonths > 0) { if ($startMonth <= 12) { echo $startMonth . ' - ' . $startYear; } else { $startMonth = 1; $startYear++; echo $startMonth . ' - ' . $startYear; } echo "\n"; $startMonth++; $distanceBetweenMonths--; } } The only thing you might need to see is if the given months are included or excluded from the calculation. The only thing missing here is the validation since it can be "skipped" of a sort if you use types inside the method.
With the DateTime class and DateInterval, you can create a generator of DateTime object of the specified range: <?php function get_all_months(int $monthstart, int $yearstart, int $monthend, int $yearend) { $onemonth = new DateInterval('P1M'); // one month interval $timeperiod = new DatePeriod( DateTime::createFromFormat('Y-m', "{$yearstart}-{$monthstart}"), $onemonth, DateTime::createFromFormat('Y-m', "{$yearend}-{$monthend}")->add($onemonth) ); foreach ($timeperiod as $pos) { yield clone $pos; } } foreach (get_all_months(12, 2009, 1, 2020) as $month) { echo "{$month->format('Y-m')}\n"; } DateTime should be more flexible to use than plain string or number array (see DateTime::format for more usage options). Note: inspired by #keidakida, updated my answer.
What is the correct way to calculate no of days between two dates in laravel
I am currently working with nepali dates. Since there is no simple logic behind end of the month and it can be 29, 30, 31 and 32. I need no of days between two date in specific logic. I have tried following code: $from = '2076-07-15'; $to = '2076-07-30'; $from_array = explode('-', $from); $to_array = explode('-', $to); $days = $to_array[2] - $from_array[2]; This gives no of days as 15 which is correct however in following case, $from = '2076-07-01'; $to = '2076-07-15'; Output will be 14 but output I want is 15 since there are 15 days from 1 to 15. Example outputs I want are: From To Required Current 2076-07-01 2076-07-15 => 15 days 14 days -----> Incorrect 2076-07-15 2076-07-30 => 15 days 15 days ------> Correct 2076-07-01 2076-07-30 => 30 days 29 days ------> Incorrect Is there any way to achieve this ? Note: There will be year and month value same for from and to variable, only day field is different
Copy and paste this code in some place accessible for you and use the last example method provided. And that will convert the Nepali into English and vice versa. And then use Carbon to subtract the dates. Nepali into english and vice versa converter <?php namespace App\Helper; class Nepali_Calendar { // Data for nepali date private $_bs = array( 0 => array(2000,30,32,31,32,31,30,30,30,29,30,29,31), 1 => array(2001,31,31,32,31,31,31,30,29,30,29,30,30), 2 => array(2002,31,31,32,32,31,30,30,29,30,29,30,30), 3 => array(2003,31,32,31,32,31,30,30,30,29,29,30,31), 4 => array(2004,30,32,31,32,31,30,30,30,29,30,29,31), 5 => array(2005,31,31,32,31,31,31,30,29,30,29,30,30), 6 => array(2006,31,31,32,32,31,30,30,29,30,29,30,30), 7 => array(2007,31,32,31,32,31,30,30,30,29,29,30,31), 8 => array(2008,31,31,31,32,31,31,29,30,30,29,29,31), 9 => array(2009,31,31,32,31,31,31,30,29,30,29,30,30), 10 => array(2010,31,31,32,32,31,30,30,29,30,29,30,30), 11 => array(2011,31,32,31,32,31,30,30,30,29,29,30,31), 12 => array(2012,31,31,31,32,31,31,29,30,30,29,30,30), 13 => array(2013,31,31,32,31,31,31,30,29,30,29,30,30), 14 => array(2014,31,31,32,32,31,30,30,29,30,29,30,30), 15 => array(2015,31,32,31,32,31,30,30,30,29,29,30,31), 16 => array(2016,31,31,31,32,31,31,29,30,30,29,30,30), 17 => array(2017,31,31,32,31,31,31,30,29,30,29,30,30), 18 => array(2018,31,32,31,32,31,30,30,29,30,29,30,30), 19 => array(2019,31,32,31,32,31,30,30,30,29,30,29,31), 20 => array(2020,31,31,31,32,31,31,30,29,30,29,30,30), 21 => array(2021,31,31,32,31,31,31,30,29,30,29,30,30), 22 => array(2022,31,32,31,32,31,30,30,30,29,29,30,30), 23 => array(2023,31,32,31,32,31,30,30,30,29,30,29,31), 24 => array(2024,31,31,31,32,31,31,30,29,30,29,30,30), 25 => array(2025,31,31,32,31,31,31,30,29,30,29,30,30), 26 => array(2026,31,32,31,32,31,30,30,30,29,29,30,31), 27 => array(2027,30,32,31,32,31,30,30,30,29,30,29,31), 28 => array(2028,31,31,32,31,31,31,30,29,30,29,30,30), 29 => array(2029,31,31,32,31,32,30,30,29,30,29,30,30), 30 => array(2030,31,32,31,32,31,30,30,30,29,29,30,31), 31 => array(2031,30,32,31,32,31,30,30,30,29,30,29,31), 32 => array(2032,31,31,32,31,31,31,30,29,30,29,30,30), 33 => array(2033,31,31,32,32,31,30,30,29,30,29,30,30), 34 => array(2034,31,32,31,32,31,30,30,30,29,29,30,31), 35 => array(2035,30,32,31,32,31,31,29,30,30,29,29,31), 36 => array(2036,31,31,32,31,31,31,30,29,30,29,30,30), 37 => array(2037,31,31,32,32,31,30,30,29,30,29,30,30), 38 => array(2038,31,32,31,32,31,30,30,30,29,29,30,31), 39 => array(2039,31,31,31,32,31,31,29,30,30,29,30,30), 40 => array(2040,31,31,32,31,31,31,30,29,30,29,30,30), 41 => array(2041,31,31,32,32,31,30,30,29,30,29,30,30), 42 => array(2042,31,32,31,32,31,30,30,30,29,29,30,31), 43 => array(2043,31,31,31,32,31,31,29,30,30,29,30,30), 44 => array(2044,31,31,32,31,31,31,30,29,30,29,30,30), 45 => array(2045,31,32,31,32,31,30,30,29,30,29,30,30), 46 => array(2046,31,32,31,32,31,30,30,30,29,29,30,31), 47 => array(2047,31,31,31,32,31,31,30,29,30,29,30,30), 48 => array(2048,31,31,32,31,31,31,30,29,30,29,30,30), 49 => array(2049,31,32,31,32,31,30,30,30,29,29,30,30), 50 => array(2050,31,32,31,32,31,30,30,30,29,30,29,31), 51 => array(2051,31,31,31,32,31,31,30,29,30,29,30,30), 52 => array(2052,31,31,32,31,31,31,30,29,30,29,30,30), 53 => array(2053,31,32,31,32,31,30,30,30,29,29,30,30), 54 => array(2054,31,32,31,32,31,30,30,30,29,30,29,31), 55 => array(2055,31,31,32,31,31,31,30,29,30,29,30,30), 56 => array(2056,31,31,32,31,32,30,30,29,30,29,30,30), 57 => array(2057,31,32,31,32,31,30,30,30,29,29,30,31), 58 => array(2058,30,32,31,32,31,30,30,30,29,30,29,31), 59 => array(2059,31,31,32,31,31,31,30,29,30,29,30,30), 60 => array(2060,31,31,32,32,31,30,30,29,30,29,30,30), 61 => array(2061,31,32,31,32,31,30,30,30,29,29,30,31), 62 => array(2062,30,32,31,32,31,31,29,30,29,30,29,31), 63 => array(2063,31,31,32,31,31,31,30,29,30,29,30,30), 64 => array(2064,31,31,32,32,31,30,30,29,30,29,30,30), 65 => array(2065,31,32,31,32,31,30,30,30,29,29,30,31), 66 => array(2066,31,31,31,32,31,31,29,30,30,29,29,31), 67 => array(2067,31,31,32,31,31,31,30,29,30,29,30,30), 68 => array(2068,31,31,32,32,31,30,30,29,30,29,30,30), 69 => array(2069,31,32,31,32,31,30,30,30,29,29,30,31), 70 => array(2070,31,31,31,32,31,31,29,30,30,29,30,30), 71 => array(2071,31,31,32,31,31,31,30,29,30,29,30,30), 72 => array(2072,31,32,31,32,31,30,30,29,30,29,30,30), 73 => array(2073,31,32,31,32,31,30,30,30,29,29,30,31), 74 => array(2074,31,31,31,32,31,31,30,29,30,29,30,30), 75 => array(2075,31,31,32,31,31,31,30,29,30,29,30,30), 76 => array(2076,31,32,31,32,31,30,30,30,29,29,30,30), 77 => array(2077,31,32,31,32,31,30,30,30,29,30,29,31), 78 => array(2078,31,31,31,32,31,31,30,29,30,29,30,30), 79 => array(2079,31,31,32,31,31,31,30,29,30,29,30,30), 80 => array(2080,31,32,31,32,31,30,30,30,29,29,30,30), 81 => array(2081,31,31,32,32,31,30,30,30,29,30,30,30), 82 => array(2082,30,32,31,32,31,30,30,30,29,30,30,30), 83 => array(2083,31,31,32,31,31,30,30,30,29,30,30,30), 84 => array(2084,31,31,32,31,31,30,30,30,29,30,30,30), 85 => array(2085,31,32,31,32,30,31,30,30,29,30,30,30), 86 => array(2086,30,32,31,32,31,30,30,30,29,30,30,30), 87 => array(2087,31,31,32,31,31,31,30,30,29,30,30,30), 88 => array(2088,30,31,32,32,30,31,30,30,29,30,30,30), 89 => array(2089,30,32,31,32,31,30,30,30,29,30,30,30), 90 => array(2090,30,32,31,32,31,30,30,30,29,30,30,30) ); private $_nep_date = array('year' => '', 'month' => '', 'date' => '', 'day' => '', 'nmonth' => '', 'num_day' => ''); private $_eng_date = array('year' => '', 'month' => '', 'date' => '', 'day' => '', 'emonth' => '', 'num_day' => ''); public $debug_info = ""; /** * Return day * * #param int $day * #return string */ private function _get_day_of_week($day) { switch ($day) { case 1: $day = "Sunday"; break; case 2: $day = "Monday"; break; case 3: $day = "Tuesday"; break; case 4: $day = "Wednesday"; break; case 5: $day = "Thursday"; break; case 6: $day = "Friday"; break; case 7: $day = "Saturday"; break; } return $day; } /** * Return english month name * * #param int $m * #return string */ private function _get_english_month($m) { $eMonth = FALSE; switch ($m) { case 1: $eMonth = "January"; break; case 2: $eMonth = "February"; break; case 3: $eMonth = "March"; break; case 4: $eMonth = "April"; break; case 5: $eMonth = "May"; break; case 6: $eMonth = "June"; break; case 7: $eMonth = "July"; break; case 8: $eMonth = "August"; break; case 9: $eMonth = "September"; break; case 10: $eMonth = "October"; break; case 11: $eMonth = "November"; break; case 12: $eMonth = "December"; } return $eMonth; } /** * Return nepali month name * * #param int $m * #return string */ private function _get_nepali_month($m) { $n_month = FALSE; switch ($m) { case 1: $n_month = "Baishak"; break; case 2: $n_month = "Jestha"; break; case 3: $n_month = "Ashad"; break; case 4: $n_month = "Shrawn"; break; case 5: $n_month = "Bhadra"; break; case 6: $n_month = "Ashwin"; break; case 7: $n_month = "kartik"; break; case 8: $n_month = "Mangshir"; break; case 9: $n_month = "Poush"; break; case 10: $n_month = "Magh"; break; case 11: $n_month = "Falgun"; break; case 12: $n_month = "Chaitra"; break; } return $n_month; } /** * Check if date range is in english * * #param int $yy * #param int $mm * #param int $dd * #return bool */ private function _is_in_range_eng($yy, $mm, $dd) { if ($yy < 1944 || $yy > 2033) { return 'Supported only between 1944-2022'; } if ($mm < 1 || $mm > 12) { return 'Error! month value can be between 1-12 only'; } if ($dd < 1 || $dd > 31) { return 'Error! day value can be between 1-31 only'; } return TRUE; } /** * Check if date is with in nepali data range * * #param int $yy * #param int $mm * #param int $dd * #return bool */ private function _is_in_range_nep($yy, $mm, $dd) { if ($yy < 2000 || $yy > 2089) { return 'Supported only between 2000-2089'; } if ($mm < 1 || $mm > 12) { return 'Error! month value can be between 1-12 only'; } if ($dd < 1 || $dd > 32) { return 'Error! day value can be between 1-31 only'; } return TRUE; } /** * Calculates wheather english year is leap year or not * * #param int $year * #return bool */ public function is_leap_year($year) { $a = $year; if ($a % 100 == 0) { if ($a % 400 == 0) { return TRUE; } else { return FALSE; } } else { if ($a % 4 == 0) { return TRUE; } else { return FALSE; } } } /** * currently can only calculate the date between AD 1944-2033... * * #param int $yy * #param int $mm * #param int $dd * #return array */ public function eng_to_nep($yy, $mm, $dd) { // Check for date range $chk = $this->_is_in_range_eng($yy, $mm, $dd); if($chk !== TRUE) { die($chk); } else { // Month data. $month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // Month for leap year $lmonth = array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); $def_eyy = 1944; // initial english date. $def_nyy = 2000; $def_nmm = 9; $def_ndd = 17 - 1; // inital nepali date. $total_eDays = 0; $total_nDays = 0; $a = 0; $day = 7 - 1; $m = 0; $y = 0; $i = 0; $j = 0; $numDay = 0; // Count total no. of days in-terms year for ($i = 0; $i < ($yy - $def_eyy); $i++) //total days for month calculation...(english) { if ($this->is_leap_year($def_eyy + $i) === TRUE) { for ($j = 0; $j < 12; $j++) { $total_eDays += $lmonth[$j]; } } else { for ($j = 0; $j < 12; $j++) { $total_eDays += $month[$j]; } } } // Count total no. of days in-terms of month for ($i = 0; $i < ($mm - 1); $i++) { if ($this->is_leap_year($yy) === TRUE) { $total_eDays += $lmonth[$i]; } else { $total_eDays += $month[$i]; } } // Count total no. of days in-terms of date $total_eDays += $dd; $i = 0; $j = $def_nmm; $total_nDays = $def_ndd; $m = $def_nmm; $y = $def_nyy; // Count nepali date from array while ($total_eDays != 0) { $a = $this->_bs[$i][$j]; $total_nDays++; //count the days $day++; //count the days interms of 7 days if ($total_nDays > $a) { $m++; $total_nDays = 1; $j++; } if ($day > 7) { $day = 1; } if ($m > 12) { $y++; $m = 1; } if ($j > 12) { $j = 1; $i++; } $total_eDays--; } $numDay = $day; $this->_nep_date['year'] = $y; $this->_nep_date['month'] = $m; $this->_nep_date['date'] = $total_nDays; $this->_nep_date['day'] = $this->_get_day_of_week($day); $this->_nep_date['nmonth'] = $this->_get_nepali_month($m); $this->_nep_date['num_day'] = $numDay; return $this->_nep_date; } } /** * Currently can only calculate the date between BS 2000-2089 * * #param int $yy * #param int $mm * #param int $dd * #return array */ public function nep_to_eng($yy, $mm, $dd) { $def_eyy = 1943; $def_emm = 4; $def_edd = 14 - 1; // initial english date. $def_nyy = 2000; $def_nmm = 1; $def_ndd = 1; // iniital equivalent nepali date. $total_eDays = 0; $total_nDays = 0; $a = 0; $day = 4 - 1; $m = 0; $y = 0; $i = 0; $k = 0; $numDay = 0; $month = array( 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); $lmonth = array( 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); // Check for date range $chk = $this->_is_in_range_nep($yy, $mm, $dd); if ( $chk !== TRUE) { die($chk); } else { // Count total days in-terms of year for ($i = 0; $i < ($yy - $def_nyy); $i++) { for ($j = 1; $j <= 12; $j++) { $total_nDays += $this->_bs[$k][$j]; } $k++; } // Count total days in-terms of month for ($j = 1; $j < $mm; $j++) { $total_nDays += $this->_bs[$k][$j]; } // Count total days in-terms of dat $total_nDays += $dd; // Calculation of equivalent english date... $total_eDays = $def_edd; $m = $def_emm; $y = $def_eyy; while ($total_nDays != 0) { if ($this->is_leap_year($y)) { $a = $lmonth[$m]; } else { $a = $month[$m]; } $total_eDays++; $day++; if ($total_eDays > $a) { $m++; $total_eDays = 1; if ($m > 12) { $y++; $m = 1; } } if ($day > 7) { $day = 1; } $total_nDays--; } $numDay = $day; $this->_eng_date['year'] = $y; $this->_eng_date['month'] = $m; $this->_eng_date['date'] = $total_eDays; $this->_eng_date['day'] = $this->_get_day_of_week($day); $this->_eng_date['nmonth'] = $this->_get_english_month($m); $this->_eng_date['num_day'] = $numDay; return $this->_eng_date; } } } // Example: // $cal = new Nepali_Calendar(); // print_r ($cal->eng_to_nep(2008,11,23)); // print_r($cal->nep_to_eng(2065,8,8)); Just you need to copy and paste and magic happend.
Try $datetime1 = new DateTime("05-07-2019"); $datetime2 = new DateTime("10-08-2021"); $difference = $datetime1->diff($datetime2); echo 'Difference: '.$difference->y.' years, ' .$difference->m.' months, ' .$difference->d.' days'; It will give an output Difference: 2 years, 1 months, 5 days If you want difference in days only.use echo 'Difference: '.$difference->format('%a').' Days'; Output will be Difference: 767 Days
The simpliest way is to use nesbot/carbon package https://carbon.nesbot.com/#gettingstarted. use Carbon\Carbon; $from = Carbon::parse('2076-07-15'); $to = Carbon::parse('2076-07-30'); $days = $from->diffInDays($to);
PHP: Get the sum of a different dates in array
Hello Im just a beginner programmer but Im now building an app which gets the total hours of work. Here I have a time_start array which the threshold for the customer to in and same as out for time_end The logs here is where the person checks in. $time_start = [ [0] => '22:00:00', [1] => '03:00:00', ]; $time_end = [ [0] => '02:00:00', [1] => '08:00:00', ]; $logs = [ [0] => '2019-07-09 22:00:00', [1] => '2019-07-10 02:00:00', [2] => '2019-07-10 03:00:00', [3] => '2019-07-10 08:00:00', ]; So I'm trying to get the sum of their hours in night shift which has different date. Im trying to get it in: foreach( $logs as $check_time ){ $attendance_date_time = date("Y-m-d",strtotime($check_time)); $time_starts = date('Y-m-d H:i:s', strtotime("$attendance_date_time $time_start")); $time_ends = date('Y-m-d H:i:s', strtotime("$attendance_date_time $time_end")); if ( $check_time >= $time_starts && $check_time <= $time_ends ) { $time[] = $check_time; }else{ $time = null } } Supposedly I will get all logs because it is between time_start and time_end and store it to $time And summing the time i get. Since I'm new at php is their any easiest idea to get the total work hours? Cuz I get a null because the time_end it gets was the date of check_in.
Here is your snippet, $time_start = [ 0 => '22:00:00', 1 => '03:00:00', ]; $time_end = [ 0 => '02:00:00', 1 => '08:00:00', ]; $logs = [ 0 => '2019-07-09 22:00:00', 1 => '2019-07-10 02:00:00', 2 => '2019-07-10 03:00:00', 3 => '2019-07-10 08:00:00', ]; $time = []; $diff = 0; for ($j = 0; $j < count($logs); $j = $j + 2) { $attendance_start = date("Y-m-d", strtotime($logs[$j])); $attendance_end = date("Y-m-d", strtotime($logs[$j + 1])); for ($i = 0; $i < count($time_start); $i++) { $time_starts = date('Y-m-d H:i:s', strtotime("$attendance_start " . $time_start[$i] . "")); $time_ends = date('Y-m-d H:i:s', strtotime("$attendance_end " . $time_end[$i] . "")); if ($logs[$j] >= $time_starts && $logs[$j + 1] <= $time_ends) { array_push($time, $logs[$j], $logs[$j + 1]); // calculating diff for two alternate days $diff += (round((strtotime($logs[$j + 1]) - strtotime($logs[$j])) / 3600, 1)); break; } } } echo $diff . " Hours";die; Demo Output:- 9 Hours
The for loop is the easiest way to do this $time_start = [ '22:00:00', '03:00:00', ]; $time_end = [ '02:00:00', '08:00:00', ]; $logs = [ '2019-07-09 22:00:00', '2019-07-10 02:00:00', '2019-07-10 03:00:00', '2019-07-10 08:00:00', ]; $totalDiff = 0; for($i =0 ; $i < count($logs); $i = $i + 2) { $startDate = strtotime($logs[$i]); $endDate = strtotime($logs[$i+1]); $diff = $endDate-$startDate; $totalDiff += $diff; } $hourDiff = $totalDiff / 3600; echo $hourDiff . ' hours'; Demo
PHP time-ago and time in the future
I have a function to get the Time-ago from a Timestamp, I grabbed it somewhere from the internet and developed and optimized it. It's fully functioning. The problem here is that it ONLY gets time-ago (from a past Timestamp), it didn't process future Timestamps (It returns 0 seconds ago). Is anybody able to help me add this functionality to the function? <?php function time_ago( $ts, $format ) { // $format = 'l, F d, Y H:i'; $granularity = 1; $dif = time() - $ts; if ( $dif < 0 ) return '0 Seconds ago'; elseif ( $dif < 604800 ) { // 604800 7 days / 864000 10 days $periods = array( 'Week' => 604800, 'Day' => 86400, 'Hour' => 3600, 'Minute' => 60, 'Second' => 1 ); $output = ''; foreach ( $periods as $key => $value ) { if ( $dif >= $value ) { $time = round( $dif / $value ); $dif %= $value; $output .= ( $output ? ' ' : '' ) . $time . ' '; $output .= ( ( $time > 1 ) ? $key . 's' : $key ); $granularity --; } if ( $granularity == 0 ) break; } // foreach( $periods as $key => $value ) return ($output ? $output : '0 seconds') . ' ago'; } else return date( $format, $ts ); } ?>
I've edited your function. Now it displays a time in the future as In 10 Hours. Code: function time_ago( $ts, $format ) { // $format = 'l, F d, Y H:i'; $granularity = 1; $dif = time() - $ts; $future = $dif < 0 ? true : false; $dif = abs($dif); if ( $dif < 604800 ) { // 604800 7 days / 864000 10 days $periods = array( 'Week' => 604800, 'Day' => 86400, 'Hour' => 3600, 'Minute' => 60, 'Second' => 1 ); $output = ''; foreach ( $periods as $key => $value ) { if ( $dif >= $value ) { $time = round( $dif / $value ); $dif %= $value; $output .= ( $output ? ' ' : '' ) . $time . ' '; $output .= ( ( $time > 1 ) ? $key . 's' : $key ); $granularity --; } if ( $granularity == 0 ) break; } // foreach( $periods as $key => $value ) if($future) { return "In " . ($output ? $output : '0 seconds'); } else { return ($output ? $output : '0 seconds') . ' ago'; } } else return date( $format, $ts ); }
This should work <?php function diffPeriods($diff) { $periods = array( 'Week' => 604800, 'Day' => 86400, 'Hour' => 3600, 'Minute' => 60, 'Second' => 1 ); $granularity = 1; $output = ''; foreach ($periods as $key => $value) { if ($diff >= $value) { $time = round($diff / $value); $diff %= $value; $output .= ($output ? ' ' : '') . $time . ' '; $output .= (($time > 1) ? $key . 's' : $key); $granularity --; } if ($granularity == 0) { break; } } return $output; } function time_ago($ts, $format) { $diff = time() - $ts; if ($diff == 0) { return 'Now'; } if ($diff < 604800 && $diff > 0) { // 604800 7 days / 864000 10 days $periods = array( 'Week' => 604800, 'Day' => 86400, 'Hour' => 3600, 'Minute' => 60, 'Second' => 1 ); $output = diffPeriods($diff); return ($output ? $output : '0 seconds') . ' ago'; } elseif ($diff < 0 && $diff > - 604800) { $output = diffPeriods($diff * - 1); return 'in ' . ($output ? $output : '0 seconds'); } // too old/new...display the date return date($format, $ts); } var_dump(time_ago(time(), 'l, F d, Y H:i')); // now var_dump(time_ago(time() + 3600, 'l, F d, Y H:i')); // 1 hour in the future var_dump(time_ago(time() + (3600 * 50), 'l, F d, Y H:i')); // 2 daysin the future var_dump(time_ago(time() + (3600 * 24 * 11), 'l, F d, Y H:i')); // 11 days in the future var_dump(time_ago(time() - 3600, 'l, F d, Y H:i')); // 1 hour ago var_dump(time_ago(time() - (3600 * 50), 'l, F d, Y H:i')); // 2 days ago var_dump(time_ago(time() - (3600 * 24 * 11), 'l, F d, Y H:i')); // 11 days ago
Combine days where opening hours are similar
How might I code a function in PHP (with CodeIgniter) to merge days with similar opening hours of a store together. For example, if we have: Mon 9am-5pm Tue 9am-5pm Wed 9am-5pm Thu 9am-5pm Fri 9am-5pm Sat 9am-7pm Sun 9am-7pm I want the code to simplify it to: Mon-Fri 9am-5pm Sat-Sun 9am-7pm How do I do this without a long list of if/else or case ifs? I'm using CodeIgniter..
<?php $openHours = array( 'Mon' => '9am-5pm', 'Tue' => '9am-5pm', 'Wed' => '9am-9pm', 'Thu' => '9am-5pm', 'Fri' => '9am-5pm', 'Sat' => '9am-7pm', 'Sun' => '9am-7pm' ); $summaries = array(); foreach ($openHours as $day => $hours) { if (count($summaries) === 0) { $current = false; } else { $current = &$summaries[count($summaries) - 1]; } if ($current === false || $current['hours'] !== $hours) { $summaries[] = array('hours' => $hours, 'days' => array($day)); } else { $current['days'][] = $day; } } foreach ($summaries as $summary) { if (count($summary['days']) === 1) { echo reset($summary['days']) . ' ' . $summary['hours'] . PHP_EOL; } else { echo reset($summary['days']) . '-' . end($summary['days']) . ' ' . $summary['hours'] . PHP_EOL; } } codepad sample
If you want it in the following format: Sun-Tue, Fri-Sat: 11am-12am; Wed: 10am-12am; Thu: 9am-12am Which will group days and handle different times within the week: <?php $open_hours = array ( "Sun" => "11am-12am", "Mon" => "11am-12am", "Tue" => "11am-12am", "Wed" => "10am-12am", "Thu" => "9am-12am", "Fri" => "11am-12am", "Sat" => "11am-12am" ); $result = []; foreach ($open_hours as $day => $hours) { if (empty($result) || $previous !== $hours) { $result[$hours][] = $day; } elseif ($previous === $hours) { $key = array_key_last($result[$hours]); $current = strtok($result[$hours][$key], '-'); $result[$hours][$key] = $current.'-'.$day; } $previous = $hours; } // build output (joining days with ,) $output = []; foreach ($result as $hours => $days) { $output[] = implode(', ', $days).': '.$hours; } // join with ;'s and output echo implode('; ', $output); https://3v4l.org/tKOlI