This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate the difference between two dates using PHP?
How to get difference between two dates in Year/Month/Week/Day?
i am trying to calculate years and months and days between two given dates in PHP.
i am also using timestamp of those date. is there any way to calculate years and months and
days from difference of those time stamp.
for example first date is 2 Jan, 2008. and second one is 5 July, 2012.
and result is 4 Years 5 monts and 3 days.
i am working on timestamp as date input and want to know that is there any function available which directly calculate above things by two input timestamp
You could use the DateTime object for that (please note the missing "," in the datetime constructor).
$datetime1 = new DateTime('2 Jan 2008');
$datetime2 = new DateTime('5 July 2012');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%y years %m months and %d days');
You can do this pretty easily with DateTime:
$date1 = new DateTime("2008-01-02");
$date2 = new DateTime("2012-07-05");
$diff = $date1->diff($date2);
echo "difference " . $diff->y . " years, " . $diff->m." months, ".$diff->d." days "
Documentation
You should have a look at Carbon, it's a pretty new PHP 5.3 lib on top of DateTime with a lot of usefull methods.
For Date diff:
<?php
$dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
$dtVancouver = Carbon::createFromDate(2013, 1, 1, 'America/Vancouver');
echo $dtOttawa->diffInHours($dtVancouver);
echo $dtOttawa->diffInDays($dtVancouver);
echo $dtOttawa->diffInMinutes($dtVancouver);
echo $dtOttawa->diffInYears($dtVancouver);
If you want Human readable diff:
$dt = Carbon::createFromDate(2011, 2, 1);
echo $dt->diffForHumans($dt->copy()->addMonth()); // 28 days before
echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 month after
You can create two DateTime objects (www.php.net/datetime) from the timestamps.
When calling the diff method you get a DateInterval object, which has properties for years and months.
Related
Calculate months and years from given value in PHP framework CodeIgniter
EMI calculator:
Here I have a total months duration, but I need to find out when is come that month and year in CodeIgniter or PHP format.
CI code update in date diff
$datetime1 = date_create(date);
$datetime2 = date_create(date);
$interval = date_diff($datetime1,$datetime2);
//echo $interval->format('%R%d days');
echo $interval->format('%y years %m months %d day basil');
This question already has answers here:
Calculating days of week given a week number
(14 answers)
Closed 6 years ago.
I am getting month and its weekno like 1,2,3,4,5 . I want ISO week from it.
Eg. I have April as month and week no 4. I want weekno 16 as output.
Thanks in advance.
You can instantiate a new DateTime using the month, increment by the number of weeks, and use the W date format.
<?php
$month = 'April';
$week = 4;
$date = new DateTime("$month 01");
$date->modify("+$week weeks");
echo $date->format('W');
As far as I can tell, the 4th week of April would be ISO week 17, not 16 though.
date("W"); Will return the week number.
http://php.net/manual/en/function.date.php
Use mktime to set a specific date / time and you'll be sorted!
This question already has answers here:
How to get the first day of a given week number in PHP (multi-platform)?
(9 answers)
Closed 9 years ago.
I have a value that is the number for the weekofyear (between 1-52 ). I want to find out the date (yyyy-mm-dd) for the last day of that week.
I would prefer to do it in PHP rathe then MYSQL.
This can be easily solved with DateTime::setISODate() method:
$week = 50;
$dt = new DateTime();
$dt->setISODate($dt->format('o'), $week, 7);
echo $dt->format('Y-m-d');
demo
Or you can just create DateTime object (or unix timestamp with strtotime()) with ISO-8601 format like 2014-W50-7:
$week = 50;
$iso = sprintf("2014-W%02d-7", $week);
$dt = new DateTime($iso);
echo $dt->format('Y-m-d');
echo date('Y-m-d', strtotime($iso)); # or using strtotime()
demo
I'm comparing dates with this code:
$date1 = new DateTime("2007-03-24 12:10:00");
$date2 = new DateTime("2009-06-26 14:00:30");
$interval = $date1->diff($date2);
If I echo this: echo $interval->m." months and".$interval->d." days."; I get the output 3 months and 2 days.. Now, I want to echo the difference between the dates but include the amount of months in the day count, so a difference of 1 month (with 30 days in it) and 5 days would be 35 days, not 1 month and 5 days. How do I do this?
I'm using PHP version 5.3+.
You should be able to use:
$interval->days;
See: http://www.php.net/manual/en/class.dateinterval.php#dateinterval.props.days
echo "There are ".$interval->days." days between the two dates.";
Your $interval variable is of type DateInterval.
Therefore, $interval->days should yeld the desired output.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to find number of days between two dates using php
If I have two dates - how do I find the real difference in days between two dates? You must take things like leap years and the number of days in each month into account.
How many days are between something like 2010-03-29 and 2009-07-16?
strtotime and simple math:
$daylen = 60*60*24;
$date1 = '2010-03-29';
$date2 = '2009-07-16';
echo (strtotime($date1)-strtotime($date2))/$daylen;
check out PHP DateTime class. It deals with all the gory details so you can just do regular subtraction.
$d1=date_create('1999-10-23');
$d2=date_create('2004-04-17');
$i=date_diff($d2,$d1);
echo $i->format('%a');
Here you go:
<?php
$date1 = strtotime("2010-03-29");
$date2 = strtotime("2009-07-16");
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
echo "Differernce is $fullDays days";
?>