Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php difference between two dates in years months and days

//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

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 Calculate the number of months between two dates

$date1 = '2000-01-25';
$date2 = '2010-02-20';

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);

$month1 = date('m', $ts1);
$month2 = date('m', $ts2);

$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
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

list of months between two dates php

$start    = (new DateTime('2010-12-02'))->modify('first day of this month');
$end      = (new DateTime('2012-05-06'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    echo $dt->format("Y-m") . "<br>
";
}
Comment

php check year and month is between two dates

$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
//echo $paymentDate; // echos today! 
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));
    
if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){
    echo "is between";
}else{
    echo "NO GO!";  
}
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

list of months between two dates php

$start    = new DateTime('2010-12-02');
$start->modify('first day of this month');
$end      = new DateTime('2012-05-06');
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    echo $dt->format("Y-m") . "<br>
";
}
Comment

PREVIOUS NEXT
Code Example
Php :: laravel old request hmtl select 
Php :: php copy file 
Php :: laravel clear all cache 
Php :: php ellipsis 
Php :: php script to generate random date 
Php :: year shortcode wordpress 
Php :: complete url php 
Php :: php get everything after last slash 
Php :: Enable / Disable modules in PHP 
Php :: how get the first item in foreach in laravel 
Php :: how to check history of database in phpmyadmin 
Php :: wp config define site url code 
Php :: dequeue beaver buillder script wordpress 
Php :: hwo to limit char in php 
Php :: wp limit post revisions 
Php :: laravel model to array 
Php :: wordpress PHPMailer config 
Php :: laravel get list of columns in a table 
Php :: take 10 character from string using php 
Php :: parent directory in php 
Php :: session_destroy not working 
Php :: encryption key has not encrypted laravel 
Php :: javascript php variable 
Php :: php round all values in array 
Php :: get product category url woocommerce 
Php :: laravel get env variable 
Php :: php get string between two strings 
Php :: Add Laravel .env variable to Vue component 
Php :: echo alert php 
Php :: redirect from index.php 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =