PHP display business hours open or closed depending on time - php

I am trying to get the minutes to work to echo "open" or "closed" depending on time. It works if i do not add the minutes, but not when adding minutes.
<?php
date_default_timezone_set('America/New_York');
$hour = (int) date('H:i');
if ($hour >= 1215 && $hour <=1735) {
// between 8:15am and 5:35pm
echo "Open";
} else {
echo "Closed";
}
?>

Given a time, for example 18:55, your date('H:i') will output a string, 18:55 when using the format H:i. If you try to cast this to an integer, like you are with (int), it will evaluate just the first number, 18.
Simply remove the colon in your date() function! This will return a string 1855 from the date, which can be cast to an integer of that value. Although, it's not strictly needed to cast it to an integer, as PHP will treat a string of numbers as an integer.
So basically you just need
$hour = date("Hi");
if ($hour >= 1215 && $hour <= 1735) {
// between 12:15 and 17:35
}

final code for future learners. thanks Qirel
<?php
date_default_timezone_set('America/New_York');
$hour = (int) date('Hi');
if ($hour >= 0815 && $hour <= 1735) {
// between 8:15am and 5:35pm
echo "Open";
} else {
echo "Closed";
}
?>

Related

Trying to make if, elseif, else statement for time and when to eat using military clock format

Whenever I put in the number 12 or 16, for example, it will not say "It's time for lunch" or "It's time for dinner." It will always say "It's time for breakfast"
I've tried putting quotes around the numbers, I've tried looking at my greater than, less than, etc. signs, and it looks like that is not the problem i think..
<?php
$time = 4;
if(($time >= 4) || ($time <= 11))
{
echo "It's time for breakfast";
}
elseif(($time >= 12) || ($time <= 15))
{
echo "It's time for lunch";
}
else
{
echo "It's time for dinner";
}
?>
I want it to run so when I make 4 - 11 in $time it will echo "It's time for breakfast." I want from 12 - 15 in $time to echo "It's time for lunch." All other numbers should print It's time for dinner.
You need to change your conditions. Right now, you're asking:
"If the time is greater than or equal to 4 OR time is less than or equal to 15..."
It will always be true if the time is greater than or equal to 4 OR less than or equal to 15 so the else if will never be reached. You must change to AND so both conditions must be met to stay in the if block.
<?php
$time = 16;
if(($time >= 4) && ($time <= 11))
{
echo "It's time for breakfast";
}
elseif(($time >= 12) && ($time <= 15))
{
echo "It's time for lunch";
}
else
{
echo "It's time for dinner";
}
?>
Its easy to get lost in many if ifelse else conditions. A simple way would I like to think about them is to think of them systematically, like this for example:
$time = 4;
if ( $time >= 15 || $time < 4 ) {
echo "It's time for dinner";
} elseif( $time >= 12 ) {
echo "It's time for lunch";
} elseif ( $time >= 4 ) {
echo "It's time for breakfast";
}

Php dates is not compare properly

I am new in PHP
when I am trying to do this
if( date('m-Y',strtotime('2016-11-01 00:00:00')) < date('m-Y') ) {
echo "yes";
} else {
echo 'no';
}
but it always do false [output 'no'].
I must need to compare months is less than current month , means compare date do not have same months
where I am wrong to compare that date ?
Use DateTime to compare dates:
$date = new DateTime('2016-11-01 00:00:00');
$now = new DateTime();
if ($date < $now && $date->format('m-Y') != $now->format('m-Y')) {
echo 'yes';
} else {
echo 'no';
}
I copied your program so that it reads:
<?php
$x=date('m-Y',strtotime('2016-11-01 00:00:00'));
echo "$x\n";
$y=date("m-Y");
echo "$y\n";
if ($x < date('m-Y') ) {
echo "yes";
} else {
echo 'no';
}
On running it the output is:
# php x.php
11-2016
01-2017
no
That is why it fails. If you are checking for just the month you need to check for equality. Otherwise you need to reorder the date formatting to be "Y-m" (not 'm-Y') for less/greater than comparisons. Comparing the strings is fine.
date function always return a string. In your if construct you compare two strings. For current time:
"11-2016" < "01-2017"
In this case "11-2016" greater than "01-2017".
It will be better to use DateTime class.
$date = new DateTime('2016-11-01 00:00:00');
$now = new DateTime();
if ($date < $now && $date->format('m-Y') != $now->format('m-Y')) {
echo 'yes';
} else {
echo 'no';
}
or in your example you need to change format to 'Y-m'.
You should use a decent format to compare the dates. Instead of m-Y, use Y-m-d.
Currently, you are converting the dates to strings, with their months first. So the first date becomes 11-2016, the second becomes 01-2017. PHP compares these as strings, and finds that 0 is less thans 1, so considers the second string to be less.

php find a stack of time

I need to find If $now(00:00:00) if included from $a and $b
echo $a= date('H:i:s', strtotime("23:00:00"));
echo $now = date('H:i:s', time("now");*(es.00:00:00)*
echo $b=date('H:i:s', strtotime("01:00:00"));
if ($now>$a && $now<$b) {echo "si";} else {echo "no";}
output say: no
__________________EDIT____________________________
Ok thanks to all, I think give you more information:
I read with php an file .xml, from this file, I get some information about program in tv, with :
-the name of program, -name of channel, -and time to program start in this format (20151210004500 +0100)
I think at this point is more easy to use this format, or not?
I would find in array from .xml the program on air, for make this I must compared the date and time in this format, It is possible?
Can you help me?
Sorry for my poor english, thanks.
Apply strtotime before comparation
if(strtotime($now) > strtotime($a) && strtotime($now) < strtotime($b)
{
// do your staff
}
$now = new DateTime;
$a = (new DateTime)->setTime(23, 0, 0);
if ($now > $a && $now < $a->modify('+2 hours')) {
echo 'si';
} else {
echo 'no';
}
You can easily do by using time() function to get the current time in numeric format (seconds from the 1970-1-1).
Than you can convert your dates into the same time format with strtotime() function.
$now = time();
$a = strtotime("10 september 2010");
$b = strtotime("10 september 2020");
if( $now > $a && $now < $b )
echo "Yes";
else
echo "No";

Get time before noon

I am practicing with dates in php. I a bit of a newbie so bear my ignorance
I am trying to see when a time is before noon.
So I have a variable coming in with this format 2014-03-07 13:28:00.000
I get the time like this
$submissonTime = date('H:i:s', strtotime($value['job_submission_date']));
then I want to set another variable as $noon and i am doing this:
$noon = date('H:i:s', '12:00:00.000');
However the value of noon is 12:00:12
what i want to do is basically:
if($submissionTime <= $noon){
//do my stuff
}
NB I want to enter the if statement when even when it is 12:00:00 and stop entering when it is 12:00:01
Any help please?
Try
$noon = date('Y-m-d 12:00:00'); // today noon with date
$submissonTime = date('Y-m-d H:i:s', strtotime($value['job_submission_date']));
if(strtotime($submissonTime) <= strtotime($noon)){
//do my stuff
}
if you want to compare only time use both format
$noon = date('12:00:00');
$submissonTime = date('H:i:s', strtotime($value['job_submission_date']));
if (date("A") == "AM")
{
// AM-Code
} else {
// PM-Code
}
Why don't you go with only one string of code getting the hour?
$Hour = date("G"); //24-hour format of an hour without leading zeros
if($Hour < 12) {
// do the code
}
Or in your case
$Hour = date("G", strtotime($value['job_submission_date']));
update
If you need 12:00:00 and not 12:00:01 and later on, you will need to define minutes and seconds:
$Hour = date("G"); //24-hour format of an hour without leading zeros
$Minute = intval(date("i")); // will give minutes without leading zeroes
$Second = intval(date("s"));
if(($Hour < 12) || ($Hour == 12 && $Minute == 0 && Second == 0)) {
// do the code
}

PHP Check if current time is before specified time

I need to check in PHP if the current time is before 2pm that day.
I've done this with strtotime on dates before, however this time it's with a time only, so obviously at 0.00 each day the time will reset, and the boolean will reset from false to true.
if (current_time < 2pm) {
// do this
}
if (date('H') < 14) {
$pre2pm = true;
}
For more information about the date function please see the PHP manual. I have used the following time formatter:
H = 24-hour format of an hour (00 to 23)
Try:
if(date("Hi") < "1400") {
}
See: http://php.net/manual/en/function.date.php
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
You could just pass in the time
if (time() < strtotime('2 pm')) {
//not yet 2 pm
}
Or pass in the date explicitly as well
if (time() < strtotime('2 pm ' . date('d-m-Y'))) {
//not yet 2 pm
}
Use 24 hour time to get round the problem like so:
$time = 1400;
$current_time = (int) date('Hi');
if($current_time < $time) {
// do stuff
}
So 2PM equates to 14:00 in 24 hour time. If we remove the colon from the time then we can evaluate it as an integer in our comparison.
For more information about the date function please see the PHP manual. I have used the following time formatters:
H = 24-hour format of an hour (00 to 23)
i = Minutes with leading zeros (00 to 59)
You haven't told us which version of PHP you're running, although, assuming it's PHP 5.2.2+ than you should be able do it like:
$now = new DateTime();
$twoPm = new DateTime();
$twoPm->setTime(14,0); // 2:00 PM
then just ask:
if ( $now < $twoPm ){ // such comparison exists in PHP >= 5.2.2
// do this
}
otherwise, if you're using one of older version (say, 5.0) this should do the trick (and is much simplier):
$now = time();
$twoPm = mktime(14); // first argument is HOUR
if ( $now < $twoPm ){
// do this
}
If you want to check whether the time is before 2.30 pm ,you can try the following code segment .
if (date('H') < 14.30) {
$pre2pm = true;
}else{
$pre2pm = false;
}
Try with
if( time() < mktime(14, 0, 0, date("n"), date("j"), date("Y")) ) {
// do this
}
This function will check if it's between hours in EST by accepting 2 params, arrays with the hour and am/pm...
/**
* Check if between hours array(12,'pm'), array(2,'pm')
*/
function is_between_hours($h1 = array(), $h2 = array())
{
date_default_timezone_set('US/Eastern');
$est_hour = date('H');
$h1 = ($h1[1] == 'am') ? $h1[0] : $h1[0]+12;
$h1 = ($h1 === 24) ? 12 : $h1;
$h2 = ($h2[1] == 'am') ? $h2[0] : $h2[0]+12;
$h2 = ($h2 === 24) ? 12 : $h2;
if ( $est_hour >= $h1 && $est_hour <= ($h2-1) )
return true;
return false;
}
Use time(), date() and strtotime() functions:
if(time() > strtotime(date('Y-m-d').' 14:00') {
//...
}

Categories