PHP - How to count how many months from a specific date - php

How can I count the number of months from the following two dates below using the Procedural style method?
PHP code.
$delete_date = "2000-01-12 08:02:39";
$current_date = date('Y-m-d H:i:s'); //current date

You're looking for DateTime::diff?
$delete_date = "2000-01-12 08:02:39";
$date_format = 'Y-m-d H:i:s';
$current_date = date($date_format);
$diff = date_diff(date_create_from_format($date_format, $delete_date), date_create());
$months = $diff->m;
Something along the lines of that.

Using DateTime you will get the total months this way:
$d1 = new DateTime("2000-01-12 08:02:39");
$d2 = new DateTime();
$d3 = $d1->diff($d2);
$months = ($d3->y*12)+$d3->m;
You would still need to handle the leftover days $d3->d ... but that depends on your needs.

$delete_date = "2000-01-12 08:02:39";
$current_date = date('Y-m-d H:i:s'); //current date
$diff = strtotime($current_date) - strtotime($delete_date);
$months = floor(floatval($diff) / (60 * 60 * 24 * 365 / 12));
echo $months . "\n";

is this what you looking for?
$delete_date = "2000-01-12 08:02:39";
$current_date = date('Y-m-d H:i:s'); //current date
// convert date to int
$delete_date = strtotime($delete_date);
$current_date = strtotime($current_date);
// calculate it
$diff = $delete_date - $current_date;
// convert int to time
$conv_diff = date('format', $diff);

Try this, it is easy, maybe not enogh chick, but very effective.
function calculateMonthsBetweenDates($fMonth, $fDay, $fYear, $tMonth, $tDay, $tYear)
{
//Build datetime vars using month, day and year
$dateFrom = mktime(0, 0, 0, $fMonth, $fDay, $fYear);
$dateTo = mktime(0, 0, 0, $tMonth, $tDay, $tYear);
//Check dateTo is a later date than dateFrom.
if($dateFrom<=$dateTo){
$yearF = date("Y", $dateFrom);
$yearT = date("Y", $dateTo);
$monthF = date("m", $dateFrom);
$monthT = date("m", $dateTo);
//same year
if ($yearF == $yearT)
$months = ($monthT - $monthF);
else{
//different year
$months = (12*($yearT-$yearF)-$monthF) + $monthT;
}
return $months;
}
else
return false; //or -1
}

Related

Add number of days to datetime and then find difference in days between two dates

I have saved a date in mysql table in date('Y-m-d H:i:s') format
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$now = date('Y-m-d H:i:s');
I want to add $blacklisted_days to $blacklisted_date
$result_date = $blacklisted_date + $blacklisted_days;
and then want to find the difference in days between the $result_date and $now.
$diff_days = $result_date - $now;
I believe this code block will help you solve the problem.
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$now = date('Y-m-d H:i:s');
$result_date = date('Y-m-d H:i:s', strtotime($blacklisted_date . '+'. $blacklisted_days.' days'));
if( $result_date > $now ){
$datediff = strtotime($result_date) - strtotime($now);
}else{
$datediff = strtotime($now) - strtotime($result_date);
}
$diff_days = round( $datediff / (60 * 60 * 24));
You can do it easily using DateTime class, look here:
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$date1 = new DateTime($blacklisted_date); // blacklisted
$date1->add(new DateInterval("P{$blacklisted_days}D")); // add N days
$date2 = new DateTime(); // now
$interval = $date1->diff($date2); // get diff
echo $interval->days; // in days
I hope it's really clear to understand

find out how many weekend in a date range

for example I have two dates 2015-10-28 and 2015-12-31. from these I want to know how many saturday and sunday in that given date range. I can find the diff between that dates but I can't find how many weekends.
anyone ever made this?
here is my current code:
function createDateRange($maxDate, $cell, $lead, $offArray = array()){
$dates = [];
--$cell;
--$lead;
$edate = date('Y-m-d', strtotime($maxDate." -$lead day"));
$sdate = date('Y-m-d', strtotime($edate." -$cell day"));
$start = new DateTime($sdate);
$end = new DateTime($edate);
$end = $end->modify('+1 day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach($period as $d){
$dt = $d->format('Y-m-d');
if(!in_array($dt, $dates)){
$dates[] = $dt;
}
}
return $dates;
}
basically I want to add sat+sun count to the date range.
The trick is to use an O(1)-type algorithm to solve this.
Given your starting date, move to the first Saturday. Call that from
Given your ending date, move back to the previous Friday. Call that to
Unless you have an edge case (where to is less than from), compute (to - from) * 2 / 7 as the number of weekend days, and add that to any weekend days passed over in steps (1) and (2).
This is how I do it in production, although generalised for arbitrary weekend days.
Use this function:
function getDateForSpecificDayBetweenDates($startDate, $endDate, $weekdayNumber)
{
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
$dateArr = array();
do
{
if(date("w", $startDate) != $weekdayNumber)
{
$startDate += (24 * 3600); // add 1 day
}
} while(date("w", $startDate) != $weekdayNumber);
while($startDate <= $endDate)
{
$dateArr[] = date('Y-m-d', $startDate);
$startDate += (7 * 24 * 3600); // add 7 days
}
return($dateArr);
}
The function call to get dates for all Sunday's in year 2015:
$dateArr = getDateForSpecificDayBetweenDates('2015-01-01', '2015-12-31', 0);
print "<pre>";
print_r($dateArr);
//周日0 周一1 .....
$data = 4;//周四
$t1 ='2015-10-28';
$t2 = '2015-12-31';
$datetime1 = date_create($t1);
$datetime2 = date_create($t2);
$interval = date_diff($datetime1, $datetime2);
$day = $interval->format('%a');
$result = ($day)/7;
$start = getdate(strtotime($t1))['wday'];
$end = getdate(strtotime($t2))['wday'];
if($data>=$start && $data<=$end){
echo floor($result)+1;
}else{
echo floor($result);
}

Receiving incorrect results from the PHP function strtotime()

I start with two dates:
$endDate = '2013-11-30 18:30:00';
$beginDate = '2013-10-31 18:30:00';
Then, I took date difference using following code:
$diff = abs(strtotime($endDate) - strtotime($beginDate));
Next, I check the date difference by
$days = $diff / (60*60*24);
However, it returns a fractional day, such as 30.041666666667. I don't want to get this fractional. Why this happening? This problem occurs in some cases only.
Try this :
$date1 = new DateTime("2013-11-21 12:59:00");
$date2 = new DateTime("2013-11-21 13:01:00");
$interval = $date1->diff($date2);
echo "DIFF: ".$interval->format("%Y-%m-%d %H:%i:%s");
Visit the link for details
you can also use mktime() function for calculate diff.
<?php
$endDate = '2013-11-30 18:30:00';
$endDateTemp = explode(' ', $endDate);
$endDateArr1 = explode('-', $endDateTemp[0]);
$endDateArr2 = explode(':', $endDateTemp[1]);
$endTimestamp = mktime($endDateArr2[0], $endDateArr2[1], $endDateArr2[2], $endDateArr1[1], $endDateArr1[2], $endDateArr1[0]);
$beginDate = '2013-10-31 18:30:00';
$beginDateTemp = explode(' ', $beginDate);
$beginDateArr1 = explode('-', $beginDateTemp[0]);
$beginDateArr2 = explode(':', $beginDateTemp[1]);
$beginTimestamp = mktime($beginDateArr2[0], $beginDateArr2[1], $beginDateArr2[2], $beginDateArr1[1], $beginDateArr1[2], $beginDateArr1[0]);
$diff = $endTimestamp - $beginTimestamp;
$day = $diff/(24*60*60);
?>

echo each date between a given value in PHP

I have a form that allows users to enter a date when they are available in the format dd/mm/yyyy.
What I want to be able to do is echo out each date between now (including today) and the date entered.
Could someone help me out? I'm not quite sure how to go about it.
For example, if someone was to enter 12/12/2011 and the date today was 10/12/2011 it would echo the following:
10/12/2011
11/12/2011
12/12/2011
Any help would be great.
$start_date = time (); // today
$end_date = strtotime ( .. your input date as string ... );
while ( $start_date + 86400 < $end_date ) {
echo date ('m/d/Y', $start_date );
$start_date += 86400; // full day
}
First of all you are using a DD-MM-YYYY, make sure your separators are - so that you don't have converting them to timestamps. Here is an example with the current format etc.
You should have more checks to make sure you do not get an infinite loop :).
$start_date = "10/12/2011";
$end_date = "12/12/2011";
print_r(dates_between($start_date, $end_date));
function dates_between($start_date, $end_date)
{
$date = array($start_date);
$start_date = str_replace('/', '-', $start_date);
$end_date = str_replace('/', '-', $end_date);
$current_date = $start_date;
while($current_date != $end_date)
{
$current_date = date("d-m-Y", strtotime("+1 day", strtotime($current_date)));
$date[] = str_replace('-', '/', $current_date);
}
return $date;
}
picked from here
<?php
$fromDate = ’01/01/2009′;
$toDate = ’01/10/2009′;
$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);
for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr = date(“Y-m-d”,$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
//print $currentDateStr.”<br />”;
}
echo “<pre>”;
print_r($dateMonthYearArr);
echo “</pre>”;
?>
//Assume dates are properly formatted.
$startDate;
$endDate;
while($startDate < $endDate){
$startDate = date("m/d/Y", strtotime("+1 day", strtotime($startDate)));
echo $startDate;
}

Elegant way to get the count of months between two dates?

Let's assume I have two dates in variables, like
$date1 = "2009-09-01";
$date2 = "2010-05-01";
I need to get the count of months between $date2 and $date1($date2 >= $date1). I.e. i need to get 8.
Is there a way to get it by using date function, or I have to explode my strings and do required calculations?
Thanks.
For PHP >= 5.3
$d1 = new DateTime("2009-09-01");
$d2 = new DateTime("2010-05-01");
var_dump($d1->diff($d2)->m); // int(4)
var_dump($d1->diff($d2)->m + ($d1->diff($d2)->y*12)); // int(8)
DateTime::diff returns a DateInterval object
If you don't run with PHP 5.3 or higher, I guess you'll have to use unix timestamps :
$d1 = "2009-09-01";
$d2 = "2010-05-01";
echo (int)abs((strtotime($d1) - strtotime($d2))/(60*60*24*30)); // 8
But it's not very precise (there isn't always 30 days per month).
Last thing : if those dates come from your database, then use your DBMS to do this job, not PHP.
Edit: This code should be more precise if you can't use DateTime::diff or your RDBMS :
$d1 = strtotime("2009-09-01");
$d2 = strtotime("2010-05-01");
$min_date = min($d1, $d2);
$max_date = max($d1, $d2);
$i = 0;
while (($min_date = strtotime("+1 MONTH", $min_date)) <= $max_date) {
$i++;
}
echo $i; // 8
Or, if you want the procedural style:
$date1 = new DateTime("2009-09-01");
$date2 = new DateTime("2010-05-01");
$interval = date_diff($date1, $date2);
echo $interval->m + ($interval->y * 12) . ' months';
UPDATE: Added the bit of code to account for the years.
Or a simple calculation would give :
$numberOfMonths = abs((date('Y', $endDate) - date('Y', $startDate))*12 + (date('m', $endDate) - date('m', $startDate)))+1;
Accurate and works in all cases.
This is another way to get the number of months between two dates:
// Set dates
$dateIni = '2014-07-01';
$dateFin = '2016-07-01';
// Get year and month of initial date (From)
$yearIni = date("Y", strtotime($dateIni));
$monthIni = date("m", strtotime($dateIni));
// Get year an month of finish date (To)
$yearFin = date("Y", strtotime($dateFin));
$monthFin = date("m", strtotime($dateFin));
// Checking if both dates are some year
if ($yearIni == $yearFin) {
$numberOfMonths = ($monthFin-$monthIni) + 1;
} else {
$numberOfMonths = ((($yearFin - $yearIni) * 12) - $monthIni) + 1 + $monthFin;
}
I use this:
$d1 = new DateTime("2009-09-01");
$d2 = new DateTime("2010-09-01");
$months = 0;
$d1->add(new \DateInterval('P1M'));
while ($d1 <= $d2){
$months ++;
$d1->add(new \DateInterval('P1M'));
}
print_r($months);
Using DateTime, this will give you a more accurate solution for any amount of months:
$d1 = new DateTime("2011-05-14");
$d2 = new DateTime();
$d3 = $d1->diff($d2);
$d4 = ($d3->y*12)+$d3->m;
echo $d4;
You would still need to handle the leftover days $d3->d if your real world problem is not as simple and cut and dry as the original question where both dates are on the first of the month.
This is a simple method I wrote in my class to count the number of months involved into two given dates :
public function nb_mois($date1, $date2)
{
$begin = new DateTime( $date1 );
$end = new DateTime( $date2 );
$end = $end->modify( '+1 month' );
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($begin, $interval, $end);
$counter = 0;
foreach($period as $dt) {
$counter++;
}
return $counter;
}
In case the dates are part of a resultset from a mySQL query, it is much easier to use the TIMESTAMPDIFF function for your date calculations and you can specify return units eg. Select TIMESTAMPDIFF(MONTH, start_date, end_date)months_diff from table_name
strtotime is not very precise, it makes an approximate count, it does not take into account the actual days of the month.
it's better to bring the dates to a day that is always present in every month.
$date1 = "2009-09-01";
$date2 = "2010-05-01";
$d1 = mktime(0, 0, 1, date('m', strtotime($date1)), 1, date('Y', strtotime($date1)));
$d2 = mktime(0, 0, 1, date('m', strtotime($date2)), 1, date('Y', strtotime($date2)));
$total_month = 0;
while (($d1 = strtotime("+1 MONTH", $d1)) <= $d2) {
$total_month++;
}
echo $total_month;
I have used this and works in all conditions
$fiscal_year = mysql_fetch_row(mysql_query("SELECT begin,end,closed FROM fiscal_year WHERE id = '2'"));
$date1 = $fiscal_year['begin'];
$date2 = $fiscal_year['end'];
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
$te=date('m',$ts2-$ts1);
echo $te;

Categories