I have the following code:
$sStart = date('Y-m-d H:i:s', strtotime($row['sStart']));
$sEnd = date('Y-m-d H:i:s', strtotime($row['sEnd']));
$sStartDate = new DateTime($sStart);
$sEndDate = new DateTime($sEnd);
$diff = date_diff($sStartDate, $sEndDate, true);
$dur = $diff->format('%h:%i');
$row['sStart'] and $row['sEnd'] are datetimes from a MySQL database.
How can I tell if $diff is over 24 hours long?
The $diff is of type DateInterval.
I think that this will give you the number of hours using the result of the date_diff:
$hours = ($diff->days * 24) + $diff->h;
if ($hours > 24) {
// etc ..
}
Since 24 hours is 1 day, you can use the %a modifier to the DateInterval::format call to calculate the number of days the interval crosses:
$sStartDate = DateTime::createFromFormat('Y-m-d H:i:s', '2013-05-13 21:56:00');
$sEndDate = DateTime::createFromFormat('Y-m-d H:i:s', '2013-05-14 21:56:01');
$diff = date_diff($sStartDate, $sEndDate, true);
$days = $diff->format('%a');
if (1 <= $days) {
echo '24 hours or more.';
} else {
echo 'less than 24 hours.';
}
See it live.
Related
I have saved a date in mysql table in date('Y-m-d H:i:s') format
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$now = date('Y-m-d H:i:s');
I want to add $blacklisted_days to $blacklisted_date
$result_date = $blacklisted_date + $blacklisted_days;
and then want to find the difference in days between the $result_date and $now.
$diff_days = $result_date - $now;
I believe this code block will help you solve the problem.
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$now = date('Y-m-d H:i:s');
$result_date = date('Y-m-d H:i:s', strtotime($blacklisted_date . '+'. $blacklisted_days.' days'));
if( $result_date > $now ){
$datediff = strtotime($result_date) - strtotime($now);
}else{
$datediff = strtotime($now) - strtotime($result_date);
}
$diff_days = round( $datediff / (60 * 60 * 24));
You can do it easily using DateTime class, look here:
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$date1 = new DateTime($blacklisted_date); // blacklisted
$date1->add(new DateInterval("P{$blacklisted_days}D")); // add N days
$date2 = new DateTime(); // now
$interval = $date1->diff($date2); // get diff
echo $interval->days; // in days
I hope it's really clear to understand
I have a system which I need to add a certain amount of fractional hours.
I've been searching and this is what I got, by far it's the most accurate method, but still doesn't give me the answer I need
function calculateHours($hours){
$now = new DateTime("2017-10-25 10:23:00");
$time = array();
$time = explode(".", $hours);
$time [1] += $time [0]*60;
$now->modify("+".$time[1]." hours");
return $now;
}
$diff = 119.23;
$answer = calculateHours($diff);
echo $answer ->format('Y-m-d H:i:s');
The answer that I want to reach is "2017-11-09 11:00:00" and I receive "2017-10-25 12:22:23" instead
Adding the hours is not correct. When you multiply hours times 60 it will make minutes.
This code should work.
function calculateHours($hours){
$now = new DateTime("2017-10-25 10:23:00");
$time = explode(".", $hours);
$time[1] += $time[0]*60;
$now->modify("+".$time[1]." minutes");
return $now;
}
$diff = 119.23;
$answer = calculateHours($diff);
echo $answer->format('Y-m-d H:i:s');
Result is 2017-10-30 09:46:00
You should use DateInterval php class to create an inverval with x seconds from your $hours variable.
Then you just have to use the datetime add interval method to modify your date
Please take a look a this example
function calculateHours($hours){
$now = new DateTime("2017-10-25 10:23:00");
var_dump($now);
$timeParts = explode(".", $hours);
// Where 23 is a percentage of on hour
$minutes = $timeParts[0] * 60 + round($time[1] * 60 / 100);
// Where 23 is the number of minutes
$minutes = $timeParts[0] * 60 + $time[1];
$interval = new DateInterval(sprintf('PT%dM', $minutes));
$now->add($interval);
echo $now->format('Y-m-d H:i:s');
return $now;
}
Use date_add
date_add($now, date_interval_create_from_date_string($tempo[1]' hours'));
or as object:
$now->add( DateInterval::createFromDateString($tempo[1].' hours'));
I'm trying to get remaining Days, hours and minutes to a certain date using php.
However i get a very strange output from my code which looks like this:
-16828 days and -11 hours and -21 minutes and -24 seconds
The future dates are stored in the mysql database in this format:
29/01/2016 7pm
So I went ahead and done this:
$Draw_time = "29/01/2016 7pm";
$date = $Draw_time;
$timestamp = strtotime($date);
$new_date = date('Y-m-d a',$timestamp );
$seconds = strtotime($new_date) - time();
$days = floor($seconds / 86400);
$seconds %= 86400;
$hours = floor($seconds / 3600);
$seconds %= 3600;
$minutes = floor($seconds / 60);
$seconds %= 60;
echo "$days days and $hours hours and $minutes minutes and $seconds seconds";
But when i run this code, I get the above strange output!
I understand that this could be because of a number reasons but the only thing i could think of is the fact that I am using a in my format?
Could someone please advise on this issue?
Simply use DateTime class like as
$Draw_time = "29/01/2016 7pm";
$date = DateTime::createFromFormat("d/m/Y ha",$Draw_time);
$date2 = new DateTime();
echo $diff = $date2->diff($date)->format("%a days and %H hours and %i minutes and %s seconds");
Try this
<?php
$Draw_time = str_replace('/', '-', "29/01/2016 7pm");
$now = new DateTime();
$futureDate = new DateTime($Draw_time);
$interval = $futureDate->diff($now);
echo $interval->format("%a days %h hours %i minutes %s seconds");
?>
Try this.
$draw_time = "2016/01/29 7pm";
$date_time = explode(" ", $draw_time);// make separate date and time in array
$date = strtotime($date_time[0]); // convert your date(2016/01/29) into php time
$time = strtotime($date_time[1]); // convert your time(7pm) into php time
$date = $date + $time; // make total time to count
$new_Date = $date - (time()); // convert into difference from current time
$day = $new_Date % 86400;
$hrs = $new_Date % 3600;
$min = $new_Date % 60;
echo "Day= ".(date("d",$day));
echo " Hours= ".(date("h",$hrs));
echo " Minutes= ".(date("i",$min));
I want to send a reminder email.I don't want to use cron on Linux/Unix/BSD box or Scheduled Tasks on Windows.
I'm trying to subtract 15 minutes from the current time.
here is my code so far (doesn't work):
$days = date("j",time());
$months = date("n",time());
$years = date("Y",time());
$hours = date("G",time());
$mins = (date("i",time()));
$secs = date("s",time());
$mins = $mins-15;
To subtract 15 minutes from the current time, you can use strtotime():
$newTime = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $newTime);
Change the date into a timestamp (in seconds) then minus 15 minutes (in seconds) and then convert back to a date:
$date = date("Y-m-d H:i:s");
$time = strtotime($date);
$time = $time - (15 * 60);
$date = date("Y-m-d H:i:s", $time);
You can use DateInterval
$date = new DateTime();
$interval = new DateInterval("PT15M");
$interval->invert = 1;
$date->add($interval);
echo $date->format("c") . "\n";
you can try this as well,
$dateTimeMinutesAgo = new DateTime("15 minutes ago");
$dateTimeMinutesAgo = $dateTimeMinutesAgo->format("Y-m-d H:i:s");
How about substracting the 15 minutes from time() before converting it?
$time = time() - (15 * 60);
And then use $time instead of time() in your code.
$currentTime = date('Y-m-d H:i:s');
$before15mins = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $before15mins);
You can also use strtotime function to subtract days, hours and/or seconds from current time.
echo date('Y-m-d H:i:s', strtotime('-15 minutes'));
Following is the way you can add days / hours / minutes / sec to current time
$addInterval = date('Y-m-d H:i:s', strtotime("+$days days $hours hours $minute minute $sec second", strtotime(currentTime)));
You can also use DateInterval object
<?php
$date = new DateTime('Y-m-d H:i:s');
$date->sub(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s');?>
Try using
$min = time() - 900; //900 seconds = 15 minutes
To subtract 15 minutes you can do:
date('Y-m-d H:i:s', (time() - 60 * 15));
You can replace 15 with the number of minutes you want.
In case you're looking to subtract seconds you can simply do:
date('Y-m-d H:i:s', (time() - 10));
In this way you'll subtract 10 seconds.
If you have only time value than below will be useful
// Your time
$time = '12:15:00';
// Returned value '12:00:00'
$newTime = date('H:i:s', strtotime($time) - (15*60));
I know this question is outdated but i just want to share how i did it in easy way
$current = new DateTime("10 minutes ago", new DateTimeZone('Asia/Manila') );
echo $current->format("Y-m-d H:i:s");
//To Get Current DateTime
$currentDate = date("Y-m-d H:i:s");
//To Get Current DateTime - 15Min
$oldDate = date("Y-m-d H:i:s", strtotime($currentDate) - (15 * 60));
echo $currentDate;
echo $oldDate;
I have two dates, formated like "Y-m-d H:i:s". I need to compare these two dates and figure out the hour difference.
You can convert them to timestamps and go from there:
$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
Dividing by 3600 because there are 3600 seconds in one hour and using round() to avoid having a lot of decimal places.
You can use DateTime class also -
$d1= new DateTime("06-08-2015 01:33:26pm"); // first date
$d2= new DateTime("06-07-2015 10:33:26am"); // second date
$interval= $d1->diff($d2); // get difference between two dates
echo ($interval->days * 24) + $interval->h; // convert days to hours and add hours from difference
As an addition to accepted answer I would like to remind that \DateTime::diff is available!
$f = 'Y-m-d H:i:s';
$d1 = \DateTime::createFromFormat($date1, $f);
$d2 = \DateTime::createFromFormat($date2, $f);
/**
* #var \DateInterval $diff
*/
$diff = $d2->diff($d1);
$hours = $diff->h + ($diff->days * 24); // + ($diff->m > 30 ? 1 : 0) to be more precise
\DateInterval documentation.
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;
You can try this :
$time1 = new DateTime('06:56:58');
$time2 = new DateTime('15:35:00');
$time_diff = $time1->diff($time2);
echo $time_diff->h.' hours';
echo $time_diff->i.' minutes';
echo $time_diff->s.' seconds';
Output:
8 hours 38 minutes 2 seconds
The problem is that using these values the result is 167 and it should be 168:
$date1 = "2014-03-07 05:49:23";
$date2 = "2014-03-14 05:49:23";
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;
$date1 = date_create('2016-12-12 09:00:00');
$date2 = date_create('2016-12-12 11:00:00');
$diff = date_diff($date1,$date2);
$hour = $diff->h;
This is because of day time saving.
Daylight Saving Time (United States) 2014 began at 2:00 AM on
Sunday, March 9.
You lose one hour during the period from $date1 = "2014-03-07 05:49:23" to
$date2 = "2014-03-14 05:49:23";
You can try this:
$dayinpass = "2016-09-23 20:09:12";
$today = time();
$dayinpass= strtotime($dayinpass);
echo round(abs($today-$dayinpass)/60/60);
You can use strtotime() to parse your strings and do the difference between the two of them.
Resources :
php.net - strtotime()