Given $some_date equal to 2015-02-12 10:28:04, how can I determine whether it is not older than X hours of the current time using PHP (and not MySQL)?
strtotime and the DateTime class are our friends. Where $x is hours:
if(($time = strtotime($some_date)) > $time + ($x * 360)) {
//do something its more than X hours
}
That likely won't work across daylight savings time boundaries, so maybe:
if(($time = strtotime($some_date)) > strtotime("+$x hours", $time)) {
//do something its more than X hours
}
This will determine if your date is less than 2 hours from the current datetime
<?php
$date = new DateTime(); //set current datetime to variable
$date->setTimezone(new DateTimeZone('America/Detroit')); //set timezone if you like
$fdate = $date->format('Y-m-d H:i:s'); //change format to year - month - day, hour, minute, seconds
$some_date = strtotime('2015-02-13 18:30:04'); // this is your datetime. use your time or change $some_date to your variable
$new_date = strtotime($fdate) - $some_date;
$minute_date = $new_date / 60; // convert to minutes
$hour_date = $minute_date / 60; // convert to hours
print $hour_date;
if($hour_date > 2){ // change the 2 to whatever you want it to be.
print "more than two hours";
}else{
print "less than two hours";
}
?>
Related
In my form there are 2 Time Pickers where user can select a from time and to time. It doesn't have a date associated with it. And for a report generating purpose I've to calculate the time difference between them. It works perfectly to if the From and to Time is "06:00 to 10:00" but if the from and to time is "21:00 to 02:00" I get a time difference of 19 hours. Could you please help me to fix this.
For this case "21:00 to 02:00" the time difference should be 5 hours.
This is the Code
$datetime1 = new \DateTime('09:30 PM');
$datetime2 = new \DateTime('02:00 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%hh');
exit;
The difference becomes negative If $totime is less than $fromtime.
DateInterval->invert == 1 indicates that. This is used with this short solution to correct the result.
$fromtime = '09:30 PM';
$totime = '02:00 AM';
$diff = date_create($fromtime)->diff(date_create($totime));
$hours = $diff->invert ? 24-$diff->h : $diff->h;
echo $hours; //5
Since you have 2 date pickers one for from time and another to time, the former will always be smaller than the latter. Hence when from time is larger than to time it means user has selected to from the next day. If we don't add a date for calculating difference, PHP will assume today's date by default. We can easily fix this by adding a condition to compare the times and prepend the dates accordingly. Below is the updated code.
<?php
$fromtime = '09:30 PM';
$totime = '02:00 AM';
$now = new \DateTime();
$today = $now->format('Y-m-d'); // Store current date
$now->add(new DateInterval('P1D')); // Add one day to current date to get next date
$nextDay = $now->format('Y-m-d'); // Store next date
if($fromtime > $totime) // If from time is bigger than to time, it means to is a next day
{
$fromdatetime = "$today $fromtime";
$todatetime = "$nextDay $totime";
}
else
{
$fromdatetime = "$today $fromtime";
$todatetime = "$today $totime";
}
$datetime1 = new \DateTime($fromdatetime);
$datetime2 = new \DateTime($todatetime);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%hh');
?>
I've got an input field where I type in a deadline in words. For example, "5 minutes", "2 days", "6 weeks", "8 months". What I want for the program to do is to calculate how long it will take when that deadline ends. And also if that deadline is almost ending, for example if 80% of the given time has passed.
I was thinking something like that php splits the given time in seconds, and then checks how many minutes and hours or days fit in those seconds and then puts that in dateTime. Like current date + input = futureDate.
I know I probably shouldn't use percentages, it's just an example.
<input type="text" name="getFutureTime">
<?php
$futureTime = $_POST['getFutureTime'];
$dateNow = date('d-m-Y H:i:s');
if($futureTime > $dateNow){
//Calculate
echo "Deadline has passed";
}else if (($futureTime / 100 * 80) < $dateNow){
//Calculate
echo "Deadline is almost passed";
}
?>
Here the values are string and you cant compare this like that. Convert them to timestamp first. Try with -
$futureTime = strtotime($_POST['getFutureTime']);
$dateNow = time();
I have a solution with, caculate how date later :)..., copy this to localhost and run :) diff
$d1=new DateTime("now");
$d2=new DateTime($_POST['DateInput']);
$diff = $d1->diff($d2);
echo "<pre>";
print_r($diff);
echo "</pre>";
$date = $diff->format('%d'); //It date
$year = $diff->format('%y'); //It year
$month = $diff->format('%m'); //It month
echo $diff->format('%a'). "<br/>";
foreach($diff as $key => $value){
echo $key . "<br/>";
echo $value;
}
$value = strtotime('14/12/2012');
echo($value);
Update
$diff->format('%a') // Is date from now example 10/12/2015 - 10/5/2015 = 7
$diff->format('%a') is 7
Try this it will work:
$date_a = new DateTime('2015-05-07 13:03:48');
$date_b = new DateTime('2015-02-04 13:03:41');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%m Months %d Days %h Hours %i Minutes %s Seconds');
I have a calendar in which the user can save events. Each event has a $start_time and an $end_time. The times are saved in HH:mm:ss format. Is there a function which calculates the differnce between the two times but takes into account only the hours, not minutes and seconds? For instance if $start_time = 09.00.00 and $end_time = 12.00.00 then difference(start_time, end_time) should be 3.
The DateTime class has a method to compare dates -
$start = new DateTime('04:13:41'); // accepts the same formats as strtotime
$end = new DateTime('08:31:13');
$diff = $start->diff($end); // pass 'true' as the second param for absolute
echo $diff->h; // 'h' contains the difference in hours
You can calculate the difference by parsing the string into a numeric format with the strtotime() function, then calculate the time delta and parse it back to a string representation with date(), like so:
$delta = strtotime($end_time) - strtotime($start_time);
$timediff = date('H', $delta);
Or you can strip out the seconds and minutes before the substraction like so:
$end_time_hrs = date('H', strtotime($end_time));
$start_time_hrs = date('H', strtotime($start_time));
And then the difference:
$delta = strtotime($end_time_hrs) - strtotime($start_time_hrs);
$timediff = date('H', $delta);
Hey i would like to know if there is any script (php) that could check if a specified date three days before today.
say..
$d1 = date("Y-m-d", filemtime($testfile));
$d2 = date("Y-m-d");
now i would like to know how to compare this two dates to check if d1 is atleast 3days ago or before d2
any help would be gladly appreciated.
Why not to use DateTime object.
$d1 = new DateTime(date('Y-m-d',filemtime($testfile));
$d2 = new DateTime(date('Y-m-d'));
$interval = $d1->diff($d2);
$diff = $interval->format('%a');
if($diff>3){
}
else {
}
Assuming you wish to test whether the file was modified more than three days ago:
if (filemtime($testfile) < strtotime('-3 days')) {
// file modification time is more than three days ago
}
Just check it with timestamp:
if (time() - filemtime($testfile) >= 3 * 86400) {
// ...
}
use date("Y-m-d", strtotime("-3 day")); for specific date
you can also use
strtotime(date("Y-m-d", strtotime("-3 day")));
to convert it to integer before comparing a date string
well, stunned to see no one is using mktime() function,
it makes the job simple
for example your input date is :10/10/2012
mktime convert it to unix time stamp
$check_date=mktime(0,0,0,10,**10+3**,2012);
we can perform any operations weather +,-,*,/
use timestamp instead of date,
$d1 = filemtime($testfile);
$now = time();
if ($now - $d1 > 3600*24*3) {
..
}
I am trying to compare times and to see if it is less than a set time and if so, do something...How can this be achieved?
For example I have this so far.
$now = strtotime(date('g:i a'));
$event_time = strtotime("10:00 am");
With these two variables, how can I check if the event_time is 5 minutes BEFORE the current "now" time?
Thanks...
You don't need strtotime for the current time. Timestamps are values in seconds, just do some math:
$now = time();
$event_time = strtotime("10:00 am");
if( ($now - $event_time) == (5 * 60)) // 5 minutes * 60 seconds, replace with 300 if you'd like
{
// 5 minutes before
}
Demo - But you should use the above code for the $now variable.