How To Get Difference between two dates

Get Difference between two dates

 

Date1 – dd-mm-yyyy
Date2 – dd-mm-yyyy

Break and get the part of the date, month and year and show the difference.

$today = date(“d-m-Y”);

$d1 = substr($d, 0,-8);
$d2 = substr($today, 0,-8);

$m1 = substr($d, 3,-5);
$m2 = substr($today, 3,-5);

$y1 = substr($d, -4);
$y2 = substr($today, -4);

$date1 = “$y1-$m1-$d1”;
$date2 = “$y2-$m2-$d2”;

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

And you can show your result anywhere on your web page like –

<?php if($days){echo $days . ” days,”;};if($months){echo $months .” months and”;};if($years){echo $years .” years”;};echo ” lefts.” ; ?>

This will be shown like –
dd days, mm months and yy years lefts.

Published by Sushant Sharma

Technologist. Software Developer. Tech Analyst ।।।

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started