Reputation: 265
I am trying to make a simple function to output 2 different lines of text depending on the time of the day. I want it to say after 4pm - Next day delivery will be processed the following day.
I have wrote this so far:
<?php
$currentTime = time() + 3600;
echo date('H:i',$currentTime);
?>
However as the date function returns a string, I am unsure of how to use an IF statement to check whether the time is greater than 16:00.
Upvotes: 20
Views: 69434
Reputation: 23
your question is answered but i will add some extra information if you are using laravel then in your formRequest Validation you can add 'estimated_completion_time' => 'required|after:today',
rule to enforce that the date created i.e estimated_completion_time
should be greater than today means its in future.
Upvotes: 0
Reputation: 51
// Happy coding
$isCurrentTimeGreater = strtotime('now') > strtotime('2022-10-13 08:15 am');
echo $timeDiff;
Upvotes: 0
Reputation: 1696
you can also check the difference between two Dates:-
<?php
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
var_dump($date1 == $date2); //bool(false)
var_dump($date1 < $date2); //bool(true)
var_dump($date1 > $date2); //bool(false)
?>
Upvotes: 1
Reputation: 1
I used this for updating something 3 times throughout the day...
$my_time= strtotime('now')-18000;//Now. My servers timezone is 5hrs ahead.
$my_day= strtotime('today')-86400; //today midnight central time.
$my_eve= $my_day +54000; //3pm central
if ($my_time > $my_day + 32400 && $my_time < $my_eve){
echo date('F j, Y, g:i a', $my_day + 32400); //9am
}elseif ($my_time > $my_eve){
echo date('F j, Y, g:i a', $my_eve); //3pm
}else{
echo date('F j, Y, g:i a', $my_day);} //midnight
Upvotes: 0
Reputation: 171
Maybe a small improvement, if you want some more accuracy:
if ( (int) date('Hi', $currentTime) > 1600 ) {
// .. do something
}
Upvotes: 1
Reputation: 1546
Try this
function checkTime($time1,$time2)
{
$start = strtotime($time1);
$end = strtotime($time2);
if ($start-$end > 0)
return 1;
else
return 0;
}
$currentTime = time() + 3600;
$time1 = date('H:i',$currentTime);
$time2 = '16:00';
if(checkTime($time1,$time2))
echo "First parameter is greater";
else
echo "Second parameter is greater";
Upvotes: 0
Reputation: 3841
if(mktime(16, 0, 0) < time()) {
echo "Next day delivery will be processed the following day.";
} else {
echo "Will be processed today.";
}
Upvotes: 1
Reputation: 78591
Compare the timestamps.
if (strtotime($date) < strtotime('16:00'))
Upvotes: 2
Reputation: 6867
Use date('H:i:s')
to get the time in a string format and compare it with "16:00:00".
Upvotes: 4
Reputation: 132071
Should do it
if (((int) date('H', $currentTime)) >= 16) {
// .. do something
}
Because PHP is weak-typed you can omit the (int)
-casting.
As a sidenote: If you name a variable $currentTime
, you shouldn't add 1 hour to it, because then its not the current time anymore, but a time one hour in the future ;) At all
if (date('H') >= 16) { /* .. */ }
Upvotes: 34
Reputation: 3485
if ($currentTime > strtotime('16:00:00')) {
// whatever you have to do here
}
Upvotes: 12