Count days between two dates in yii2

calculate days from current date in php
calculate remaining days in php
php difference between two dates in days
php calculate time difference between two dates in hours and minutes
how to calculate number of weeks between two dates in php
date between in php
compare two dates in php
php days until date

Today I am counting days between two dates in yii2. One date is on column of database. Database column is due_date. The other date is current date.

Here is the code for what I done so far in yii2 :

$abc=Yii::$app->db->createCommand('select * from lib_chekout where patron_id=:patron_id   AND is_checkedin=0')
   ->bindValue(':patron_id',$patron_id)
   ->queryAll();


$datetime1 =  $abc[0]['due_date'];
$datetime2 =  date("Y-m-d H:i:s");
$interval = $datetime1->diff($datetime2);

print_r($interval);
exit;

My due_date is no $abc array . Now how can I count the dates ?

For reference due_date has date 2011-08-13 00:00:00. And today date is 2016-12-13.

How can I do it ?

as @kashif said you can use date_diff function. first you need to convert you date to DateTimeInterface then pass it to date_diff.

//PARA: Date Should In YYYY-MM-DD Format
//RESULT FORMAT:
// '%y Year %m Month %d Day %h Hours %i Minute %s Seconds'        =>  1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds
// '%y Year %m Month %d Day'                                    =>  1 Year 3 Month 14 Days
// '%m Month %d Day'                                            =>  3 Month 14 Day
// '%d Day %h Hours'                                            =>  14 Day 11 Hours
// '%d Day'                                                        =>  14 Days
// '%h Hours %i Minute %s Seconds'                                =>  11 Hours 49 Minute 36 Seconds
// '%i Minute %s Seconds'                                        =>  49 Minute 36 Seconds
// '%h Hours                                                    =>  11 Hours
// '%a Days                                                        =>  468 Days
//////////////////////////////////////////////////////////////////////
function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
    $datetime1 = date_create($date_1);
    $datetime2 = date_create($date_2);

    $interval = date_diff($datetime1, $datetime2);

    return $interval->format($differenceFormat);

}

from here

how to calculate difference between two dates - Yii 1.0.x , $days = $diff / 60 / 60 / 24. </pre>. yes thanks i got it. pandiraja (C Pandit) September 6, 2018, 6:02am #4. how to subtract between two different  Today I am counting days between two dates in yii2. One date is on column of database. Database column is due_date. The other date is current date. Here is the code for what I done so far in yii2

DateTime::diff -- DateTimeImmutable::diff -- DateTimeInterface::diff -- date_diff — Returns the difference between two DateTime objects

You must convert your date string into datetime object

$abc = Yii::$app->db->createCommand('select * from lib_chekout where patron_id=:patron_id   AND is_checkedin=0')
        ->bindValue(':patron_id',$patron_id)
        ->queryAll();


$datetime1 =  new Datetime($abc[0]['due_date']);
$datetime2 =  new Datetime(date("Y-m-d H:i:s"));
$interval = $datetime1->diff($datetime2)->days;


print_r($interval);
exit;

Demo

How to Get Number of Days Between Two Dates in PHP, How can I get days between two dates in PHP? Today I am counting days between two dates in yii2. One date is on column of database. Database column is due_date. The other date is current date.

Try this way. $datetime1 and $datetime2 should be DateTime object.

$datetime1 = new DateTime('2011-08-13 00:00:00');
//$datetime1 = new DateTime($abc[0]['due_date']);
$datetime2 = new DateTIme('Now');
$interval = $datetime1->diff($datetime2);

print_r($interval->format('%R%a'));

PHP cal_days_in_month() Function, How get number of days in a month in PHP? Date Calculators. Duration Between Two Dates – Calculates number of days. Time and Date Duration – Calculate duration, with both date and time included; Date Calculator – Add or subtract days, months, years; Birthday Calculator – Find when you are 1 billion seconds old; Related Links. Date/calendar related services – Overview

Program to find the number of days between two dates in PHP , The task is to find the number of days between the given dates. Examples: Input : date1 = "17-09-2018" date2 = "31-09-2018" Output: Difference  Duration Between Two Dates – Calculates number of days. Date Calculator – Add or subtract days, months, years. Birthday Calculator – Find when you are 1 billion seconds old.

How to count days between two dates in PHP?, In this tutorial, we will write about calculating the number of days between two dates. This can be done very easily using the DateTime object  How many days, months, and years are there between two dates? Count Days Add Days Workdays Add Workdays Weekday Week №. From and including: Saturday, June 6, 2020. To, but not including Saturday, June 6, 2020.

How will you calculate days between two dates in PHP? -, Calculating days between two dates in PHP: See how to calculate the number of days between two dates in PHP. Date Calculator: Add to or Subtract From a Date. Enter a start date and add or subtract any number of days, months, or years.

Comments
  • use date_diff($date1,$date2);
  • got an error :: PHP Warning 'yii\base\ErrorException' with message 'date_diff() expects parameter 1 to be DateTimeInterface, string given'
  • $date1 and $date2 must be in date format use $date1=strtotime($date1); $date2=strtotime($date2);