Receiving incorrect results from the PHP function strtotime() - php

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);
?>

Related

Days between two dates doesn't work in PHP

I need to compute the days between two dates (format YYYYMMDD)
I used two test dates, 2020-01-20 and 2020-02-20
$enddate = "20200220";
$startdate = "20200120";
$s = new DateTime($enddate);
$e = new DateTime($startdate);
$diff = $s->diff($e);
echo "days: ".$diff->d;
the result is 0 instead of being a month worth of days
days: 0
I understand that dates as strings might be ambiguous, so I also tried to specify the format, by doing:
$enddate = "20200220";
$startdate = "20200120";
$s = DateTime::createFromFormat('Ymd', $startdate);
$e = DateTime::createFromFormat('Ymd', $enddate);
$diff = $s->diff($e);
echo "days: ".$diff->d;
Still got 0
days: 0
You can use strtotime
<?php
// strtotime converts any string date format to unix time
$date1 = "2020-01-20";
$date2 = "2020-02-20";
$seconds_in_a_day = 86400;
$diff = (strtotime($date2) - strtotime($date1))/$seconds_in_a_day;
echo $diff; // output 31

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

Subtract two dates to display number of Days

<?php
$datetime1 = new DateTime("$da[tofollowon]");
$datetime2 = new DateTime();
$difference = $datetime1->diff($datetime2);
$days = $difference->days;
?>
Above php code displays difference in two dates, but i want to subtract these two dates datetime2 from datetime1 which should even display negative values.
Try this
$d1 = strtotime("2014-01-10"); // or your date as well
$d2 = strtotime("2014-01-01");
$datediff = $d1 - $d2;
echo floor($datediff/(60*60*24)).' days';
try
$start = strtotime('2010-01-25');
$end = strtotime('2010-02-20');
$days_between = ceil(abs($end - $start) / 86400);
For more :- Finding the number of days between two dates

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

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
}

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