Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php calculate date difference

//get Date diff as intervals 
$d1 = new DateTime("2018-01-10 00:00:00");
$d2 = new DateTime("2019-05-18 01:23:45");
$interval = $d1->diff($d2);
$diffInSeconds = $interval->s; //45
$diffInMinutes = $interval->i; //23
$diffInHours   = $interval->h; //8
$diffInDays    = $interval->d; //21
$diffInMonths  = $interval->m; //4
$diffInYears   = $interval->y; //1

//or get Date difference as total difference
$d1 = strtotime("2018-01-10 00:00:00");
$d2 = strtotime("2019-05-18 01:23:45");
$totalSecondsDiff = abs($d1-$d2); //42600225
$totalMinutesDiff = $totalSecondsDiff/60; //710003.75
$totalHoursDiff   = $totalSecondsDiff/60/60;//11833.39
$totalDaysDiff    = $totalSecondsDiff/60/60/24; //493.05
$totalMonthsDiff  = $totalSecondsDiff/60/60/24/30; //16.43
$totalYearsDiff   = $totalSecondsDiff/60/60/24/365; //1.35
Comment

Get the number of days between two dates in PHP

$startDate = new DateTime("2019-10-27");
$endDate = new DateTime("2020-04-11");

$difference = $endDate->diff($startDate);
echo $difference->format("%a");
Comment

display date period between two date php

/**
 * takes two dates and returns an array of dates between them
 * 
 * @param date1 The start date of the period.
 * @param date2 The end date of the period.
 * @param format The format of the date you want to display.
 * 
 * @return array array of dates between the two dates.
*/
function displayDatePeriod( $date1, $date2 , $format = "Y-m-d"){

    $periodArray = [];
    $period = 
      new DatePeriod(
      new DateTime($date1),
      new DateInterval('P1D'),
      new DateTime($date2)
    );

    foreach ($period as $key => $value) {
      $periodArray[] = $value->format($format) ;      
    }
  
  	$periodArray[] = $date2 ;

    return $periodArray;
}
1
Comment

how to calculate days difference between two dates in php

// how to calculate days difference between two dates in laravel

use DateTime; // inside Controller Class

$startDate = new DateTime($request->start_date);
$endDate   = new DateTime($request->end_date);

$daysDifference = ($startDate->diff($endDate)->days);
Comment

php difference between two dates

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days
", $years, $months, $days);
Comment

How to calculate the difference between two dates php

$datetime1 	= new DateTime('2020-10-11 16:52:52');
$datetime2 	= new DateTime('2020-10-13 16:52:52');
$interval 	= $datetime1->diff($datetime2);

echo $interval->format('%a days');

$interval = $datetime1->diff($datetime2);
$diffInSeconds = $interval->s; 
$diffInMinutes = $interval->i; 
$diffInHours   = $interval->h; 
$diffInDays    = $interval->d; 
$diffInMonths  = $interval->m; 
$diffInYears   = $interval->y; 
Comment

Calculate the Difference Between Two Dates Using PHP

phpCopy$firstDate = "2019-01-01";
$secondDate = "2020-03-04";

$dateDifference = abs(strtotime($secondDate) - strtotime($firstDate));

$years  = floor($dateDifference / (365 * 60 * 60 * 24));
$months = floor(($dateDifference - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days   = floor(($dateDifference - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 *24) / (60 * 60 * 24));

echo $years." year,  ".$months." months and ".$days." days";

//output: 1 year, 2 months and 3 days
Comment

php get date between two dates

$period = new DatePeriod(
     new DateTime('2010-10-01'),
     new DateInterval('P1D'),
     new DateTime('2010-10-05')
);

//Which should get you an array with DateTime objects. 

//To iterate

foreach ($period as $key => $value) {
    //$value->format('Y-m-d')       
}
Comment

find days with name between two dates in php

$from_date ='01-01-2013';
$to_date ='05-01-2013';

$from_date = new DateTime($from_date);
$to_date = new DateTime($to_date);

for ($date = $from_date; $date <= $to_date; $date->modify('+1 day')) {
  echo $date->format('l') . "
";
}
Comment

get number of days between two dates php

//get Date diff as intervals 
$d1 = new DateTime("2018-01-10 00:00:00");
$d2 = new DateTime("2019-05-18 01:23:45");
$interval = $d1->diff($d2);
$diffInSeconds = $interval->s; //45
$diffInMinutes = $interval->i; //23
$diffInHours   = $interval->h; //8
$diffInDays    = $interval->d; //21
$diffInMonths  = $interval->m; //4
$diffInYears   = $interval->y; //1
Comment

get number of days between two dates php

$now = time(); // or your date as well
$your_date = strtotime("2010-01-31");
$datediff = $now - $your_date;

echo round($datediff / (60 * 60 * 24));
Comment

php get all days between two dates

$period = new DatePeriod(
     new DateTime('2010-10-01'),
     new DateInterval('P1D'),
     new DateTime('2010-10-05')
);

foreach ($period as $key => $value) {
    //$value->format('Y-m-d')       
}
Comment

php check if date between two dates

Current date is between two dates
Comment

PREVIOUS NEXT
Code Example
Php :: Unable to create PsySH runtime directory. Make sure PHP is able to write to /run/user in order to continue. 
Php :: how to store file in public folder laravel 
Php :: add item to array in php 
Php :: php to list files 
Php :: curl get response headers php 
Php :: how to save the variable from query in mysql with php 
Php :: How to get the current taxonomy term ID (not the slug) in WordPress? 
Php :: Woocommerce - Adding a Custom Endpoint 
Php :: push key value array php 
Php :: add access-control-allow-origin header laravel 
Php :: laravel create or update eloquesnt 
Php :: laravel upgrade php version 
Php :: redirect to attempting url after login laravel 
Php :: Disable wordpress editor - gutenberg on Post type post 
Php :: avg rating get in join in laravel 8 
Php :: Convert String containing commas to array 
Php :: laravel create model and migration 
Php :: action after model is created laravel 
Php :: encrypt/decrypt data php 
Php :: insert data using seeder in laravel 
Php :: laravel validation get failed rules 
Php :: laravel pluck relationship 
Php :: symfony migration down 
Php :: php nginx file not found 
Php :: laravel loop iteration 
Php :: php if in array 
Php :: wordpress post types supports thumbnail 
Php :: laravel invoice number generator 
Php :: cache an array 
Php :: laravel return view with multiple variable 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =