This question already has answers here:
Finding the number of days between two dates
(34 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 months ago.
i am beginner at php. I want to calculate exact total number of days between two dates using php, mysql and want to show it in html form. I tried datediff but it doesn't works as it gives , diffrence not total number of days.
Date fm - 10-10-22;
Date to - 20-10-22;
Total Days - 11
add + 1 to date_diff the output will 11 be like :
<?php
$d1=date_create("2022-10-10");
$d2=date_create("2022-10-20");
$diff=date_diff($d1,$d2);
echo $result = 1 + $diff->format("%R%a days");
?>
php has inbuilt DateTime functions to get the difference. Below outputs 10 day difference.
$date = new DateTime('2022-10-20');
$next_date = new DateTime('2022-10-30');
echo $date->diff($next_date)->days;
Related
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
How to Calculate time difference between two dates in php?
$date1=date('Y-m-d H:i:s');
$date2=date('2015-03-06 45:06:03');(Db)
am trying to do once time difference between 2 hours send Mail to user.Current datetime and Db datetime difference between 2 hours mean Send Mail to user.How to Check?
Did you try $diff=$date1 - $dates?
strtotime — Parse about any English textual datetime description into a Unix timestamp and divide by 3600 to find result in hours. Try like this..
$date1=date('Y-m-d H:i:s');
$date2=date('2015-03-06 45:06:03');
echo $diff_hours = (strtotime($date1) - strtotime($date2))/3600;
This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 7 years ago.
Hi I'm still new in PHP and currently I want to make system about Employee Allowance and have no idea how to start calculate months in php .For example Steve should get allowance for 4 months from now and database stored the result of calculation, so far the coding is:
?php
$date=date_create("2013-03-15");
date_add($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
?>
Months input is from user and stored in database.
I'm not sure I follow your 40 days versus 4 months, but I think you want something like this:
echo date('Y-m-d', strtotime("Now +40 days"));
This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Finding the number of days between two dates
(34 answers)
Closed 8 years ago.
I want to calculate days between 2 month.1 date is set that is 01-03-2014 and second date will be given by user.This may be any date for example 14-05-2014.Now i want to calculate days between these 2 dates?
Thanks
You can use DateTime for this, and do a simply calculation. See DateTime:diff
<?php
$start= new DateTime('14-05-2014');
$end= new DateTime('01-03-2014');
$interval = $start->diff($end);
echo $interval->format("%a total days"); //Output: 74 total days
https://eval.in/236630
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
how to calculate the minute difference for 2 values:-
strtotime("-15 minutes");
strtotime("now");
i have used below code:-
$diff = floor(strtotime("now") - strtotime("-15 minutes")/3600 );
but it gives 5 days of difference.
please guide me where I am wrong.
First of all you are getting hour diff with division to 3600. 15 minutes evaluates to 0 when floored. Also you are only dividing the second variable not the first with those brackets.
this should work:
$diff=floor((strtotime("now") - strtotime("-15 minutes"))/60);
echo $diff;// outputs 15
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP iterate days of month given month and year
"Obtain all days in month with the number of months"
For example i have date 2011/12/16 this date have months 12(december) now i want with code php know that, This month(12 or december) is a few days?(29? or 31? or 30?)
How is it by PHP?
$mydate = strtotime('2011/12/16');
$daysInMonth = date('t',$mydate);
Edited to add strtotime()