I tried to find out difference between today date and specific day with format Ymd.
How to check whether specific day is greater than 30 days from today?
For example:
$date1 = '20160315'; // 2016-03-15
$date2 = '20160115'; // 2016-01-15
Try this
$date1=date_create('20160315');
$date2=date_create('20160115');
$diff=date_diff($date1,$date2);
$days = $diff->format("%a");
if($days > 30) do something
So simple...
$date1 = '20160315'; // 2016-03-15
$date2 = date(Ymd); // 2016-01-15
$day_difference = $date1 - $date2
if($day_difference > 30) {
echo 'specific day is greater than 30 days from today';
} else {
echo 'specific day is less than 30 days from today';
}
Try this:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-11-13');
$interval = $datetime1->diff($datetime2);
$int = $interval->format('%R%a');
if($int > +30) {
echo "Greater than 30 days";
} else {
echo "Less than 30 days";
}
Related
I am creating PHP function that will return difference between two dates in a format: 2 Months, 3 Weeks, 6 Days, 3 Hours. I have tried to use PHP DateTime class, but it returns only Months, Days and Hours and I can not find a way to calculate Weeks.
This is my function:
public function DateTimeDifference($FromDate, $ToDate) {
$FromDate = new DateTime($FromDate);
$ToDate = new DateTime($ToDate);
$Interval = $FromDate->diff($ToDate);
$Difference["Hours"] = $Interval->h;
$Difference["Days"] = $Interval->d;
$Difference["Months"] = $Interval->m;
return $Difference;
}
Now, I need $Difference["Weeks"] also included in return data.
EDIT: I know I can divide Days with 7 and get weeks, but this does not result right. For example: 2 Months, 14 Days, 3 Hours - When I divide 14 days with 7 I will get this: 2 Months, 2 Weeks, 14 Days, 3 Hours and now this is not same period.
public function DateTimeDifference($FromDate, $ToDate) {
$FromDate = new DateTime($FromDate);
$ToDate = new DateTime($ToDate);
$Interval = $FromDate->diff($ToDate);
$Difference["Hours"] = $Interval->h;
$Difference["Weeks"] = floor($Interval->d/7);
$Difference["Days"] = $Interval->d % 7;
$Difference["Months"] = $Interval->m;
return $Difference;
}
// this will only work from previous dates
// difference between utc date time and custom date time
function FromUtcToCustomDateTimeDifference($ToDate)
{
// Takes Two date time
$UTC_DATE = new DateTime('now', new DateTimeZone('UTC'));
$UTC_DATETIME = $UTC_DATE->format('Y-m-d H:i:s');
// add your own date time 1
$datetime1 = date_create(UTC_DATETIME);
$datetime2 = date_create($ToDate);
$Interval = date_diff($datetime1, $datetime2);
// Count Number Of Days Difference
$Day = $Interval->format('%a');
if($Day > 1)
{
$Month = $Interval->format('%m');
$Year = $Interval->format('%y');
$Week = (int)($Interval->format('%a')/7);
if($Year<1)
{
if($Day <= 7)
{
return $Day > 1 ? $Day.= " days ago" : $Day .= " day ago";
}
else if($Month<1)
{
return $Week > 1 ? $Week.= " weeks ago" : $Week .= " week ago";
}
return $Month > 1 ? $Month.= " months ago" : $Month .= " month ago";
}
else
{
return $Year > 1 ? $Year.= " years ago" : $Year .= " year ago";
}
}
else
{
return "today";
}
}
I'm having trouble getting "weeks" in DateTime::diff function
Here's my code:
$date1 = new DateTime("2017-05-14");
$date2 = new DateTime("2017-06-14");
$interval = $date1->diff($date2);
echo $interval->m.' '.($interval->m > 1 ? 'months' : 'month');
It worked if I'm going to get the "month" count, but I want to get the weeks before turning it into a month:
We have 4 weeks in a month (4.34524 to be exact from Google Unit Converter), if the difference between start date and the date today exceeds 4 weeks, it should output "1 month" and so on..
Code (Demo):
$date1 = new DateTime("2017-06-1");
$date2 = new DateTime("2017-06-15");
$interval = $date1->diff($date2);
//var_export($interval);
if($interval->m>0){ // check if >= 1 month
echo "{$interval->m} month",($interval->m>1?'s':'');
}else{
$weeks=floor($interval->days/7); // if not yet 1 month, calc weeks
echo "$weeks week",$weeks!=1?'s':'';
}
// output: 2 weeks
Calculate days and then divide by 7 for week.
Try this code :
$date1 = new DateTime("2017-05-14");
$date2 = new DateTime("2017-06-14");
$interval = $date1->diff($date2);
$week = floor($date1->diff($date2)->days/7);
echo $week;
if($week > 4)
{
echo $interval->m.' '.($interval->m > 1 ? 'months' : 'month');
}
you could do this to get weeks and then make the condition:
$daysInAweek = 7;
$weeks = ($interval->days)/$daysInAweek;
if($weeks >= 4) {
echo 'is a month';
}
because a week have 7 days.
I have a date time difference-
I need to calculate those days also into hour.
$start = '2016-05-26 19:05:00';
$end = '2016-05-29 17:05:00';
$datetime1 = new DateTime($start);
$datetime2 = new DateTime($end);
$interval = $datetime1->diff($datetime2);
echo $interval->format("%h Hours %i Min %s Sec"); //22 Hours 0 Min 0 Sec
I know this:
echo $interval->format('%d days');
I try this:
echo $interval->format("(%h + %d * 24) Hours %i Min %s Sec"); //(22 + 8 * 24) Hours 0 Min 0 Sec
$start_date = new DateTime('2007-09-01 04:10:58');
$since_start = $start_date->diff(new DateTime('2012-09-11 10:25:00'));
echo $since_start->days.' days total<br>';
echo $since_start->y.' years<br>';
echo $since_start->m.' months<br>';
echo $since_start->d.' days<br>';
echo $since_start->h.' hours<br>';
echo $since_start->i.' minutes<br>';
echo $since_start->s.' seconds<br>';
echo (($since_start->days)*24)+$since_start->h.' Total Hours<br>';
Use this claculate the days and then multiple by 24 plus remaining hours
Its simple, all though it cannot be done automatically for you, you have to write code.
Here is all you need:
$start = '2016-05-26 19:05:00';
$end = '2016-05-29 17:05:00';
$datetime1 = new DateTime($start);
$datetime2 = new DateTime($end);
$interval = $datetime1->diff($datetime2);
// Below this line is where the magic takes place:
$hourDiff = ($interval->d*24)+($interval->h); //here we calculate the hours
echo $hourDiff //prints 77 because 2 days*24 hours each + 22 hours = 77
In my example I also take into account the years and months (y and m respectively) but you can delete those if you don't want them.
I have two dates like
2016-01-25 07:33:54 and current date 30-01-2016 01.27.00
I want to get difference between these dates with dates in number of hours, number of minutes and number of seconds .
$fromDate = "2016-01-25 07:33:54";
$toDate = "30-01-2016 01.27.00";
$difference = strtotime($toDate)-strtotime($fromDate);
echo "Seconds : ".$difference."<br>";
echo "Minutes : ".($difference/60)."<br>";
echo "Hours : ".($difference/(60*60));
Or
A Easy way
$fromDate = "2016-01-25 07:33:54";
$toDate = "30-01-2016 01.27.00";
$datetime1 = new DateTime($toDate);
$datetime2 = new DateTime($fromDate);
$interval = $datetime1->diff($datetime2);
$days = $interval->d;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;
echo "$days days, $hours Hrs, $minutes Mins, $seconds Sec";
http://php.net/manual/en/function.strtotime.php
use strtotime to find time difference between two dates.
http://php.net/manual/en/class.datetime.php
You can also try this.
checkout my code below.
<?php
$datetime1 = new DateTime('2014-02-16 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days')." days ".$interval->format('%h')." Hours ".$interval->format('%i')." Minutes".$interval->format('%s')." Seconds";
?>
Calculate between two Dates duration in PHP
Example :
Start Date : 01-01-2016
End Date : 31-01-2016
I get Answer is 30 days but I want result is 1 month
More Examples:
01-01-2016 to 31-01-2016 = 1 month
01-02-2016 to 29-02-2016 =1 month
01-03-2016 to 31-03-2016 =1 month
show on ..
<?php
$date1 = '2000-01-20';
$date2 = '2000-02-20';
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
echo $diff = (($year2 - $year1) * 12) + ($month2 - $month1);
?>
I think this will help you.
Here is what I tried.
Find the first and last day of the month of first date and compare them with the given dates. If they matches it prints "1 month" else it prints the number of days like "n days".
$date1 = "2016-02-01";
$date2 = "2016-02-29";
$output = "";
if ($date1 == date("Y-m-01", strtotime($date1)) && $date2 == date("Y-m-t", strtotime($date1))) {
$output = "1 month";
} else {
$output = ((strtotime($date2) - strtotime($date1)) / 86400 + 1) . " days";
}
echo $output;
class shahjadclass {
public function getduration($postdate) {
/* $postdate is the date where the post was created */
$assigned_time = date("Y-m-d h:i:sa");
$d1 = new DateTime($assigned_time);
$d2 = new DateTime($postdate);
$interval = $d2->diff($d1);
$datestring='';
if($interval->y>0){
$datestring=$interval->format('%y Years ago') ;
}elseif ($interval->m>0) {
$datestring=$interval->format('%m Months ago') ;
}elseif ($interval->d>0) {
$datestring=$interval->format('%d Days ago') ;
}elseif ($interval->h>0) {
$datestring=$interval->format('%h Hours ago') ;
}elseif($interval->i>0){
$datestring=$interval->format('%i Min ago') ;
}else{
$datestring=$interval->format('%s Sec ago') ;
}
return $datestring;
}
}
**
Usage
require_once('shahjadclass.php');
$myfunctions = new shahjadclass();
To get Output
$datestring=$myfunctions->getduration('createdon');
**