Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php greater than certain time

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.

like image 330
Glynn Avatar asked Jun 16 '11 10:06

Glynn


People also ask

How can I compare two times in PHP?

The trick to manipulating and comparing dates and times in PHP is to store date/time values in an integer variable and to use the mktime(), date() and strtotime() functions. The integer repesentation of a date/time is the number of seconds since midnight, 1970-Jan-1, which is referred to as the 'epoch'.

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


2 Answers

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) { /* .. */ }
like image 95
KingCrunch Avatar answered Oct 20 '22 17:10

KingCrunch


if ($currentTime > strtotime('16:00:00')) {
    // whatever you have to do here
}
like image 37
mkilmanas Avatar answered Oct 20 '22 16:10

mkilmanas