PHP - Calculate Days Between Two Dates
Hello all,
The exact thing that i need is to calculate how much days there is in between two dates. The only problem is that every thing that i found dont care about leap year Anyone have a function to do that? Similar TutorialsI've tried several scripts to get the number of days between two dates, but none of them will work. Obviously I'm doing something wrong. One thing I know that is causing a problem is that I'm using variables instead of an actual typed in date. Which in my opinion is how most people would do it - variables not actual dates. I've tried these: $todaydate = date('Y/m/d'); $now = date('Y-m-d'); $dd2 = strtotime($now); $thisyear = 2019; $payment_day = 29; $pay_month = 6; if($pay_month == 1){$newpaymentmonth = 2;} if($pay_month == 2){$newpaymentmonth = 3;} if($pay_month == 3){$newpaymentmonth = 4;} if($pay_month == 4){$newpaymentmonth = 5;} if($pay_month == 5){$newpaymentmonth = 6;} if($pay_month == 6){$newpaymentmonth = 7;} if($pay_month == 7){$newpaymentmonth = 8;} if($pay_month == 8){$newpaymentmonth = 9;} if($pay_month == 9){$newpaymentmonth = 10;} if($pay_month == 10){$newpaymentmonth = 11;} if($pay_month == 11){$newpaymentmonth = 12;} if($pay_month == 12){$newpaymentmonth = 1;} $threezero = array(4, 6, 9, 11); // months with 30 days // Deal with months that have only 28 days or 30 days, setting the payment day to accommodate months with fewer days. if ($payment_day > 28 && $pay_month == 2) { $payment_day = 28; } elseif ($payment_day == 31 && in_array($pay_month, $threezero)) { $payment_day = 30; } else { $payment_day = $payment_day; } if ($newpaymentmonth == 1){$newpaymentyear = $thisyear + 1;}else{$newpaymentyear = $thisyear;} $newpaymentdate = date($newpaymentyear.'-'.$newpaymentmonth.'-'.$payment_day); echo date("Y-m-d", $newpaymentdate) . "<br><br>"; $ddate = new DateTime($thisyear.'-'.$newpaymentmonth.'-'.$payment_day); $ddate->add(new DateInterval('P5D')); echo $ddate->format('Y-m-d') . " - New month payment date with 5 day grace period added.<br><br>";
Now, how to calculate the difference in days between today's date and $ddate? I tried the below, but none worked. function DateDiff($strDate1,$strDate2){ return (strtotime($strDate2) - strtotime($strDate1))/ ( 60 * 60 * 24 ); // 1 day = 60*60*24 } echo "Date Diff = ".DateDiff($now,$ddate)."<br>"; $timeDiff = abs($now - $ddate); $numberofdays = $timeDiff/86400; echo "<p>$numberofdays - days between today's date and payment w/grace date.</p>"; $date1 = $now; $date2 = $ddate; $diff = date_diff($date1,$date2); echo 'Days Count - '.$diff->format("%a"); $date1=$now; $date2=$ddate; function dateDiff($date1, $date2) { $date1_ts = strtotime($date1); $date2_ts = strtotime($date2); $diff = $date1_ts - $date2_ts; return round($diff / 86400); } $dateDiff= dateDiff($date1, $date2); printf("Difference between in two dates : " .$dateDiff. " Days "); print "</br>"; This one returns 18112 Days. Should be days 7 days from 2019-08-05. None of these work, so I'm doing something wrong. Any ideas? Thanks Edited August 9, 2019 by cyberRobotfixed typo I have date stored in database in any of the given forms 2020-06-01, 2020-05-01 or 2019-04-01 I want to compare the old date with current date 2020-06-14 And the result should be in days. Any help please? PS: I want to do it on php side. but if its possible to do on database side (I am using myslq) please share both ways🙂 Edited June 14, 2020 by 684425Hi, I need to calculate absent percentage but not sure how. working days from Sunday to Thursday every week so 5 working days a week. i will calculate the absent days from joining date until current date. Example if joining date is 24-3-2020 and today's date is 7-4-2020 i should get the number of absent days to 11 days since both Friday and Saturday are excluded. How to do that? here is what I have: $workingdays = $curdate - $joindate; $workingdays = $workingdays - $absent = ($counter / $workingdays) * 100; the second line i missing the number of days for (Fridays and Saturdays) that should be excluded from calculations. The third line i did calculate the actual working days ($counter) for the employee so then i can get percentage of absent days. How to exclude the weekend days from calculations? Edited April 1, 2020 by ramiwahdanmistake in dates Hi, I have this code :- $datestarted = $row['datestarted']; $datestart=date("l d/m/Y @ H:i:s",strtotime($datestarted)); Which is been echo'd like this :- echo ' <tr align="center"'.$rowbackground.'> <td>'.$datestart.'</td> <td align="center"> '; So for example I get this output :-Sunday 18/04/2021 @ 10:45:26 The data in the DB for the above is stored like this :-2021-04-18 10:45:26
What I'd like to do is also echo how many days ago this date was, all of the examples I've tried don't seem to work though? Hi all, I am trying to figure out how to calculate 5 working days prior to a given date. I have done some googling but can only see examples of how to add 5 working days onto a date, such as this: Code: [Select] $holidayList = array(); $j = $i = 1; while($i <= 5) { $day = strftime("%A",strtotime("+$j day")); $tmp = strftime("%d-%m-%Y",strtotime("+$j day")); if($day != "Sunday" and $day != "Saturday" and !in_array($tmp, $holidayList)) { $i = $i + 1; $j = $j + 1; } else $j = $j + 1; } $j = $j -1; echo strftime("%A, %d-%m-%Y",strtotime("+$j day")); Does anyone know how to calculate 5 working days prior to a date? Many thanks, Greens85 Hi there, i am using a form with 2 inputs which are equipped with a datepicker: Date 1 & Date 2, is it possible to calculate how many days are there from Date 1 to Date 2 (including the selected ones) ? On my form the dates are in this format: September 08, 2011 (i guess i can change that to numeric only, if that helps) Tamper data shows them getting posted like this: September+14%2C+2011 Any help / hints will be appreciated ! Hi All, I need to subtract dates and display the number of days left. I have a 'Start' date and an 'End' date in DATETIME format in the DB. Not quite sure where to start. A simply start - end doesn't work . Start = 2011-11-01-00:00:00 End = 2011-11-30-23:59:59 Since it is now 2011-11-27, my output should equal 3. Any help is appreciated. Hi guys, I am trying to do a multidates events availability calender. The script below indicates todays date by highlighting an orange colour and also indicates the start and end date of the event highlighting grey colour on the two dates (The colour are link via css classes as shown). Code: [Select] //Today's date $todaysDate = date("d/m/Y"); $dateToCompare = $daystring . '/' . $monthstring . '/' . $year; echo "<td align='center' "; if($todaysDate == $dateToCompare){ echo "class='today'"; }else{ //Compare's the event dates $sqlcount = "select event_start,event_end from b_calender where event_start ='".$dateToCompare."' AND event_end='".$dateToCompare."'"; $noOfEvent = mysql_num_rows(mysql_query($sqlcount)); if($noOfEvent >= 1){ echo "class='event'"; } } It works ok i.e. if start date = 01/01/2012 and end date = 04/01/2012 both date will be highlighted with grey colour. However I want it to also highlight grey on the dates between the 1st and 4th to show that then anydates between the 1st and 4th are not available and this is when I'm stuck. Please guys I need help. Thanks I'm trying to figure out if today's current date and time falls between Mon & Fri, but so far, I can't get it to work. Can anybody look at my code and see what I'm doing wrong? Code: [Select] <?php $day_start = date('D h:i a', strtotime("Mon 05:30")); $day_end = date('D h:i a', strtotime("Fri 10:00")); $day_current = date('D h:i a', strtotime("+1 hours")); if (($day_current > $day_start) && ($day_current < $day_end)) { echo "yup"; } else { echo "nope"; } ?> Thanks in advance Hey there, Thanks for taking the time to read my thread. My issue is if I'm given a time stamp in PHP how could I calculate the number of days until that time stamp?. Thanks for your time. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=320501.0 Hello, I am trying to put together a mysql query that will return the number of visitors for four days ago, what i am trying to do is plot the last seven days visitors on a graph in the format of day seven, day six, etc and need to find away to get a count for each of those days. At the moment i am thinking about running various queries with each returning the results for a specific day. The code below is supposed to get the count of visitors four days ago. Not between now and four days ago but just for the 24 hour period which covers day 4. The current code just returns a value of 0. Any help would be appreciated. Code: [Select] $result = mysql_query("SELECT ip FROM ip_stats WHERE date= date_sub(NOW(), interval 4 DAY)"); $num_rows = mysql_num_rows($result); echo "$num_rows"; I have a SQL row that has a date field: ex: 2010-11-01. When a car is sold there either is a 30 day warranty, a 60 day warranty, or 0 day warranty. What I'm trying to do is display when the vehicles warranty expires, based on the date it was sold, or when did it expire based on the same sold date pulled from the database. Example using last months date: 2010-10-01 60 day: "Expires 11-30-10" 30 day: "Expired 11-01-10" I can not seem to use the date function properly... Any help would be greatly appreciated. Hello. I want to generate the initial of the day of all the days of a chosen month. Meaning, if I choose this month it will give me: TFSSMTWTFSSMTWTFSSMTWTFSSMTWTF Is that clear? Can anybody help me? thanks! Hi there, for every case we have "gooo" as commen in all how this could be one code using anything like elseif or switch whatever... if($refere && (stripos($r, $refere) === false)){ echo "x1" }else{ echo"goooo" } And if($limited && ($line[hits] >= $limited)){ echo "x2" }else{ echo"goooo" } And if($pword && (isset($_POST['password']) && $_POST['password'] == $pword) ){ echo"goooo" }else{ echo"x3" } And if ($line[capt] == 1 && ($_SESSION["security_code"]) ){ echo"goooo" }else{ echo"x4" } thanks in advance Hi, I have db table that records the days (Sunday, Monday...) when the employee login to the system. Say that the employee logged in on Monday then Logged in on Wednesday so this means he was absent on Tuesday. I calculated the number of days to get the answer 2 (between Wednesday and Monday before logging in on Wednesday) how to get the name of the missing day "Tuesday"? code: $curdate = date("Y-m-d"); $currday = date('l'); $lastdate = $row['indate']; $lastdayin = $row['lastdayin']; if ($lastdate !=Null) { $misseddates = strtotime($curdate) - strtotime($lastdate); $misseddates = $misseddates / 86400; echo $misseddates; $misseddays = strtotime($lastdayin) - strtotime($currday); $misseddays = $misseddays / 86400; echo $misseddays; } I tried to get the name of day in the last step but it only calculates 5 in numbers how to get the name. I want the answer to be "Tuesday" as of the example. Thanks. Hi I am trying to add a field to a database that is 4 days from the date the record is added, but it is not adding a value Code: [Select] $end_date=strtotime("+ 4 days"); $add_vehicle_sql=mysql_query("INSERT INTO `tbl_auction_lot`(`cust_id`,`reserve`,`make`,`model`,`spec`,`fuel`,`doors`,`mot_date`,`fns`,`fos`,`rns`,`ros`,`condition`,`reg_no`,`service_history`,`sale_type`,`status`,`keepers`,`gearbox`,`emissions`,`colour`,`date_first_reg`,`date_manufacture`,`bhp`,`engine_size`,`end_date`) VALUES ('$seller_id','$reserve','$make','$model','$body_style','$fuel_type','$no_of_doors','$mot','$fns','$fos','$rns','$ros','$vehicle_condition','$vrm','$service_history','auction','$status','$prev_keepers','$gearbox','$emissions','$colour','$date_reg','$date_man','$bhp','$engine_size','$end_date')") or die(mysql_error()); What am I doing wrong and what is there a better way to achieve the desired result. When I try to add 30 days: Code: [Select] $date = date("Y-m-d"); $date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days"); echo $date; and I echo date I get 1330664400 How do I get it to echo out 3/1/2012? I know the answer lies in the strtotime but I can't figure it out. I know it's a simple problem for most of you... please could some one help me i need to limit the query by timestamp for the last 7 days here is my code on the php side Code: [Select] <?php $db = @mysql_connect("localhost", "8conv", "*****") or die("Connection Error: " . mysql_error()); mysql_select_db("tempmonitor") or die("Error connecting to db."); $sql = "SELECT *, UNIX_TIMESTAMP(datetime) AS datetime FROM 8conv"; $result = mysql_query($sql); $data = array(); while ($row = mysql_fetch_array($result)) { $temp1[] = array($row['datetime'] * 1000, (int)$row['temp1']);; } $temp1 = json_encode($temp1); How can I check in php if a timestamp was x days ago For example the timestamp 1287677304 , how can I check if it was more than 2 days ago? Thanks lots Jake |