<?php
$time = date('Hi');
if ($time > 0030) {
echo "closed";
} elseif ($time >= 0800) {
echo "open";
}
?>
This is the code i'm using for a client's website. In short it's a code that'll show if the client's business is open or not.
My client's working hours are between 8am to 12:30am.
I was wondering if there's an easier way of doing this or am I doing this right?
Try this:
<?php
date_default_timezone_set('Asia/Calcutta'); # set timezone according to your requirement
$now = new DateTime();
$start = new DateTime('today 08:00:00');
$end = new DateTime('tomorrow 00:30:00');
if ($start <= $now && $now < $end) {
echo "open";
}
else{
echo "close";
}
?>
Related
Now I'm doing an internship in a local company. The company gave me a task; I have to be able to make a Telegram bot, but before that, I don't know how to get time & data that has been input.
I'm using Sublime Text for code and MySQL.
$start = strtotime('2019-07-26 00:00:00');
$end = strtotime('2019-07-26 03:00:00');
$time = strtotime($r_smr['TO_CHAR']);
$time = substr($r_smr['TO_CHAR'], 11);
echo $time;
if ($time >= $start && $time <= $end) {
echo "ok";
} else {
echo "not ok";
}
I expect the output is the time & date that already input, but the result isn't what I expected.
Try to do this:
$start = strtotime('2019-07-26 00:00:00');
$end = strtotime('2019-07-26 03:00:00');
$time= strtotime('2019-07-26 02:00:00'); //used test datetime
//$time= substr($r_smr['TO_CHAR'], 11);
echo $time; //echo timestamp 1564124400
if($time >= $start && $time <= $end) {
echo "ok"; //echo ok
} else {
echo "not ok";
}
Hope it helps
in my database table i have stored start_at and end_at timings of hotels
Here is my slim api php code
` $st_time = strtotime('Start_at');
$end_time = strtotime('end_at');
$stmt->execute();
$data=$stmt->fetchAll( PDO::FETCH_ASSOC);
$cur_time= time();
if($st_time < $cur_time && $end_time > $cur_time)
{
echo $res='open ';
}
else
{
echo $res='Close';
}
`
You can do string comparisons and it will work in PHP. This is what will help you in the right direction...
$st_time = '07:30:00';
$end_time = '22:30:00';
$cur_time = date('H:i:s');
if($st_time < $cur_time && $end_time > $cur_time){
echo 'Open';
} else {
echo 'Close';
}
I have a case where a box of text should not be shown between two times for example 20 and 01 (24-hour clock), but it should also work, when i choose not to show the box betweem 20 and 22 for example.
But if I have:
$start = "20";
$end = "01";
$now = date('H');
if($now > $start AND $now < $end) {
echo "DONT SHOW THE BOX";
} else {
echo "SHOW THE BOX";
}
How can I convert the numbers, can I use mktime() even if I don't have a date? Because the box should be activated every day in that time range.
You don't want to show the box between 20:00 to 01:00
Currently your logic is kinda messed up in if($now > $start AND $now < $end).
If you expect $start = 20 and $end = 1, Then what kind of value is $now that might be MORE than 20 AND LESS than 1.
Your if statement logic will always go to ELSE whatever the value of $now is.
But there's another workaround to switch the logic like this.
You want to show the box between 02:00 to 19:00
Instead of the other way around.
So you can do this,
$start = "20";
$end = "01";
$now = date('H');
if ($now > $end && $now < $start)
{
echo "SHOW THE BOX";
}
else
{
echo "DON'T SHOW";
}
Update 1:
Now, you don't want to show the box between 20:00 to 22:00
You can do the vice versa or which is your current logic. Like,
$start = "20";
$end = "22";
$now = date('H');
if ($now >= $start && $now <= $end)
{
echo "DON'T SHOW";
}
else
{
echo "SHOW THE BOX";
}
Update 2:
If the $start or $end varies, you can always wrap them in another if condition. Like,
if ($start > $end)
{
if ($now > $end && $now < $start)
{
echo "SHOW THE BOX";
}
else
{
echo "DON'T SHOW";
}
}
else if ($start < $end)
{
if ($now >= $start && $now <= $end)
{
echo "DON'T SHOW";
}
else
{
echo "SHOW THE BOX";
}
}
The code isn't very pretty, but I think this is what you are after?
define('START_TIME', 6);
define('END_TIME', 1);
$startTime = new DateTime();
$startTime->setTime(START_TIME, 0);
$endTime = clone $startTime;
if (START_TIME > END_TIME) {
$endTime->modify('+1 day');
}
$endTime->setTime(END_TIME, 0);
$currentTime = new DateTime();
if ($currentTime > $startTime && $currentTime < $endTime) {
echo 'Show box';
} else {
echo 'Don\'t show box';
}
You can alter the hours to decide the start/end hour for when to show/hide. If the ending hour is a lower hour than the starting hour then it will presume the ending hour should be the next day.
It could potentially be cleaned up a bit, but I'll leave that in your hands if it is what you're after.
date_default_timezone_set("Europe/Stockholm");
$setdate = ['14-05-2022','15-05-2022','16-05-2022'];
print_r($setdate)."<br>";
foreach ($setdate as $value) {
$date= date("d-m-Y");
if($date == $value){
// echo "set Data For Product";
// die();
}
}
I have to check if the current daytime falls in a specific range. I looked up the internet and found several similar solutions like this one:
$now = date("His");//or date("H:i:s")
$start = '130000';//or '13:00:00'
$end = '170000';//or '17:00:00'
if($now >= $start && $now <= $end){
echo "Time in between";
}
else{
echo "Time outside constraints";
}
If both conditions have to be true, how can this bis achieved when we assume that $start is 06:00:00 and $end is 02:00:00.
If we make the assumption that it is 01:00:00, in this case the first condition can't be true.
Has anybody an idea to handle this problem differently?
Thanks!
Naturally, you'd have to account for date in your comparisons.
<?php
$start = strtotime('2014-11-17 06:00:00');
$end = strtotime('2014-11-18 02:00:00');
if(time() >= $start && time() <= $end) {
// ok
} else {
// not ok
}
If you need to check whether or not the time frame rolls over midnight
function isWithinTimeRange($start, $end){
$now = date("His");
// time frame rolls over midnight
if($start > $end) {
// if current time is past start time or before end time
if($now >= $start || $now < $end){
return true;
}
}
// else time frame is within same day check if we are between start and end
else if ($now >= $start && $now <= $end) {
return true;
}
return false;
}
You can then get whether or not you are within that time frame by
echo isWithinTimeRange(130000, 170000);
date_default_timezone_set("Asia/Colombo");
$nowDate = date("Y-m-d h:i:sa");
//echo '<br>' . $nowDate;
$start = '21:39:35';
$end = '25:39:35';
$time = date("H:i:s", strtotime($nowDate));
$this->isWithInTime($start, $end, $time);
function isWithInTime($start,$end,$time) {
if (($time >= $start )&& ($time <= $end)) {
// echo 'OK';
return TRUE;
} else {
//echo 'Not OK';
return FALSE;
}
}
Cannot comment due to low reputation, but #DOfficial answer is great but be aware of inconsistency in comparision.
Original
// if current time is past start time or before end time
if($now >= $start || $now < $end){
Should be imho
// if current time is past start time or before end time
if($now >= $start || $now <= $end){
My goal to display a message if time range is between given range If not display another one.
I tried;
date_default_timezone_set("Europe/Istanbul");
$saat = date("h:i");
if ($saat <='08:00' && $saat >='22:00') {
echo ('yes we are open');
}
else ('sorry we are closed');{
}
I know i make mistake while trying to get that if time is between the range, but i cannot overcome problem.
waiting for your responses.
Try the following.
$saat = new DateTime();
$open = new DateTime( $saat->format('Y-m-d').' 08:00',new DateTimeZone('Europe/Istanbul'));
$close = new DateTime($saat->format('Y-m-d').' 22:00',new DateTimeZone('Europe/Istanbul'));
if (($saat >= $open) && ($saat <= $close)) {
echo 'yes we are open';
}else{
echo 'sorry we are closed';
}
Try this
date_default_timezone_set("Europe/Istanbul");
$saat = date("h:i");
if ($saat <='08:00' && $saat >='22:00')
{
echo 'yes we are open';
}
else
{
echo 'sorry we are closed';
}
Its better to perform a less than / greater than operation on a DateTime object. Also I think you have your >= and <= confused, and you have an extra bracket.
I changed the name of the $saat variable to $now to make it more understandable.
date_default_timezone_set("Europe/Istanbul");
//Create a DateTime Object represent the date and time right now
$now = new DateTime();
//Today's Opening Date and Time
$open = new DateTime( $now->format('Y-m-d').' 08:00' );
//Today's Closing Date and Time
$close = new DateTime( $now->format('Y-m-d').' 22:00' );
if ($now >= $open && $now <= $close) {
echo ('yes we are open');
}
else ('sorry we are closed');{
}
On a side note, I NEVER use date() because of the 2038 problem (google it). DateTime is not subject to such problems.
If you don't care about the minutes you can do it like this
date_default_timezone_set("Europe/Istanbul");
$saat = date("h");
if ($saat<=8 && $saat>=22) {
echo ('yes we are open');
} else {
echo('sorry we are closed');
}
you can use your condition like
if (strtotime($saat) <=strtotime('08:00') && strtotime($saat) >=strtotime('22:00'))
Ok here is the code :
date_default_timezone_set("Asia/Karachi");
$t=time();
//echo(date("H:i",$t)); // Current Time
$hour = (date("H",$t)); // Current Hour
$minute = (date("i",$t)); //Current Minute
if (($hour <= 8)&&($hour >= 22)) {
echo "We are open";
}
else {
echo "sorry we are closed";
}