I want to set a fix time variables in php for my if and else condition.
For example:
$startTime = '08:00:00';
$endTime = '16:00:00';
$totalhrs = $endTime - $startTime;
echo $totalhrs;
Anyone know how to declare the time in PHP?
Thanks for the help
$startTime = strtotime('08:00:00');
$endTime = strtotime('16:00:00');
$totalhrs = ($endTime - $startTime) / 3600;
echo $totalhrs;
you can use datetime object for this case
$startTime = new DateTime('08:00:00');
$endTime = new DateTime('16:00:00');
$totalhrs = $startTime->diff($endTime)->h;
You can try the below function to check timestamps. If you don't pass it a second parameter, it will evaluate if the first time has passed the CURRENT time, otherwise it will compare the first time against the second.
Function timeHasPassed($Time, $Time2 = 0) {
If ($Time2 != 0) {
$Now = new DateTime($Time2);
} Else {
$Now = new DateTime();
}
$Then = new DateTime($Time);
If ($Now > $Then) {
Return TRUE;
} Else {
Return FALSE;
/*
You can also use the below code to print out how long is left until the timestamp has passed. Keep in mind, this will return TRUE if tested as a boolean so maybe consider returning a different datatype instead of TRUE if you decide to go this route.
$Time = new DateTime($Time);
$Now = new DateTime();
$Remainder = $Time->diff($Now);
$Remainder = $Remainder->format("%h hours, %i minutes, and %s seconds!");
return $Remainder;
*/
}
}
$start = '22:00:00';
$end = '08:00:00';
$now = Carbon::now('UTC');
How can I check if the time of $now is within the timerange?
There are several ways to achieve that by using Carbon. One of the easiest ways is using createFromTimeString and between methods:
$now = Carbon::now();
$start = Carbon::createFromTimeString('22:00');
$end = Carbon::createFromTimeString('08:00')->addDay();
if ($now->between($start, $end)) {
// ¯\_(ツ)_/¯
}
Try this:
$time = Carbon::now();
$morning = Carbon::create($time->year, $time->month, $time->day, 8, 0, 0); //set time to 08:00
$evening = Carbon::create($time->year, $time->month, $time->day, 18, 0, 0); //set time to 18:00
if($time->between($morning, $evening, true)) {
//current time is between morning and evening
} else {
//current time is earlier than morning or later than evening
}
The true in $time->between($morning, $evening, true) checks whether the $time is between and including $morning and $evening. If you write false instead it checks just if it is between the two times but not including.
Actually, you could leave true away because it is set by default and not needed.
Check here for more information on how to compare dates and times with Carbon.
$start = '22:00:00';
$end = '08:00:00';
$now = Carbon::now('UTC');
$time = $now->format('H:i:s');
if ($time >= $start && $time <= $end) {
...
}
Should do it, but doesn't take date into consideration
You can reverse check algorithm.
<?php
$pushChannel = "general";
$now = Carbon::now();
$start = Carbon::createFromTime(8, 0);
$end = Carbon::createFromTime(22, 0);
if (!$now->between($start, $end)) {
$pushChannel = "silent";
$restrictStartTime = Carbon::createFromTime(22, 0, 0); //carbon inbuild function which will create todays date with the given time
$restrictEndTime = Carbon::createFromTime(8, 0, 0)->addDays(1); //this will create tomorrows date with the given time
$now = Carbon::now();
if($now->gt($restrictStartTime) && $now->lt($restrictEndTime)) {
.....
}
Please Try below code,
$start = '22:00:00';
$end = '08:00:00';
$now = Carbon::now('UTC');
$nowTime = $now->hour.':'.$now->minute.':'.$now->second;
if(strtotime($nowTime) > strtotime($start) && strtotime($nowTime) < strtotime($end) ) {
echo 'YES';
} else {
echo 'NO';
}
What Chris is trying to point out is if the endtime crosses over midnight then you must account for that.
This is not the cleanest way to do it but here is a method that seems to work.
private function isNowBetweenTimes($timezone, $startDateTime, $endDateTime) {
$curTimeLocal = Carbon::now($timezone);
$startTime = $curTimeLocal->copy();
$startTime->hour = $startDateTime->hour;
$startTime->minute = $startDateTime->minute;
$endTime = $curTimeLocal->copy();
$endTime->hour = $endDateTime->hour;
$endTime->minute = $endDateTime->minute;
if ($endTime->lessThan($startTime))
$endTime->addDay();
return ($curTimeLocal->isBetween($startTime, $endTime));
}
This example only cares about the hour and minutes and not the seconds but you can easily copy that as well. The key to this is comparing start and end time before comparing them to the current time and add a day to end time if end time is less than start time.
For complete solution which supports all start and end time range you can use bitwise XOR.
/*
* must using hours in 24 hours format e.g. set 0 for 12 pm, 6 for 6 am and 13 for 1 pm
*/
private $startTime = '0';
private $endTime = '6';
$currentHour = \Carbon\Carbon::now()->hour;
$start = $this->startTime > $this->endTime ? !($this->startTime <= $currentHour) : $this->startTime <= $currentHour;
$end = $currentHour < $this->endTime;
if (!($start ^ $end)) {
//Do stuff here if you want exactly between start and end time
}
an updated version of #AliN11's answer taking into account ranges accross two days or in the same day
$now = now();
$start = Carbon::createFromTimeString('22:00');
$end = Carbon::createFromTimeString('08:00');
if ($start > $end) {
$end = $end->addDay();
}
if ($now->between($start, $end)||$now->addDay()->between($start, $end)) {
//add statements
}
<?php
$now = date("H");
if ($now < "20") {
echo "Have a good day!";
}
Try this :
$start = 22; //Eg. start hour
$end = 08; //Eg. end hour
$now = Carbon::now('UTC');
if( $start < $now->hour && $now->hour < $end){
// Do something
}
#AliN11's (currently top) answer is good, but doesn't work as one would immediately expect, after midnight it just breaks, as raised in the comments by #Sasha
The solution is to reverse the logic, and check if the time is not between the inverse hours.
Here is an alternative that works as one would expect:
$now = Carbon::now();
$start = Carbon::createFromTimeString('08:00');
$end = Carbon::createFromTimeString('22:00');
if (! $now->between($start, $end)) {
// We're all good
}
Yes, the midnight plays a vital role in time duration. We can find now() being the given time range as follows:
$now = Carbon::now();
$start = Carbon::createFromTime('22', '00');
$end = Carbon::createFromTime('08', '00');
if ($start->gt($end)) {
if ($now->gte($start)) {
$end->addDay();
} elseif ($now->lte($end)) {
$start->subDay();
} else {
return false;
}
}
return $now->between($start, $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";
}
<?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";
}
?>
I have some DB entries, they have timestamps. And all I want is to draw a separate line between days. And also, I need the day to start not at 00:00, but at 07:00. It's like an offset for the day start.
Now I have that (for context):
foreach($logs as $log) {
$cur_date = $log[0]['timestamp'];
echo "<p>".$log[0]['content']."</p>";
}
Is there a simple workaround for the problem I've described?
Thank you!
$prev_date = 0;
foreach($logs as $log) {
$cur_date = strtotime($log[0]['timestamp']);
$cur_day_beginning = strtotime(date("Y-m-d 07:00:00", $cur_date));
if ($cur_date >= $cur_day_beginning && $prev_date < $cur_day_beginning) {
echo "<hr/>";
}
$prev_date = $cur_date;
echo "<p>".$log[0]['content']."</p>";
}