how to check if two datetime between current times - php

i wanna check if current time is between current day 8AM and next day 2AM
i did try
$currentTime = date('h:i A', time());
$startTime = "8:00 AM";
$endTime = "2:49 AM";
if ((strtotime($currentTime) >= strtotime($startTime)) && (strtotime($currentTime) <= strtotime($endTime))) {
// do something
}
what should result
true
but its return
false

If dates are part of your logic, then use them. You can utilize DateTime's relative formats:
$start = new DateTime('today 08:00 AM');
$end = new DateTime('tomorrow 02:00 AM');
$current = new DateTime();
if ($start <= $current && $current <= $end) {
// do something
}

I updated your code a bit and it working
$currentTime = date('h:i A'); //UTC time
$startTime = "8:00 AM";
$endTime = "11:50 PM";
if ((strtotime($currentTime) >= strtotime($startTime)) && (strtotime($currentTime) <= strtotime($endTime))) {
print 'test';
}

Related

PHP print dates without sunday

I have this form with this input field
<input type="text" name="txt_datetimein" class="form-control datetime">
<input type="text" name="txt_datetimeout" class="form-control datetime">
<input type="text" name="txt_lenght" class="form-control">
I enter the first date and the second date and the length the length i precise the number of repetition
than i click next and i have all days without sunday
example if i put 5 in the length and datetimein 01-04-2017 8:00:00 and datetimeout is 01-04-2017 5:00:00
the output will be like that
Date IN Date Out Day
01-04-2017 8:00:00 01-04-2017 5:00:00 Saturday
03-04-2017 8:00:00 03-04-2017 5:00:00 Monday
04-04-2017 8:00:00 04-04-2017 5:00:00 Tuesday
05-04-2017 8:00:00 05-04-2017 5:00:00 Wednesday
06-04-2017 8:00:00 06-04-2017 5:00:00 Thursday
07-04-2017 8:00:00 07-04-2017 5:00:00 Friday
this my code but it's print all day
<?php
for($i=0;$i<=$lenght;$i++) {
$date = date('m/d/Y H:i:s', strtotime("+$i days", strtotime($datetimein)));
$edate = date('m/d/Y H:i:s', strtotime("+$i days", strtotime( $datetimeout)));
$is_sunday = date('l', strtotime($date)) == 'Sunday';
if ($is_sunday) {
$day = date('l', strtotime("+1+$i days",strtotime($datetimein)));
}
else {
$day = date('l', strtotime("+$i days",strtotime($datetimein)));
}
}
?>
How Can i solve my Problem ??!!
try this below code
$datetimein = "01-04-2017 8:00:00";
$datetimeout = "01-04-2017 5:00:00";
$lenght = 20;
for($i=0;$i<=$lenght;$i++) {
$date = date('m/d/Y H:i:s', strtotime("+$i days", strtotime($datetimein)));
$edate = date('m/d/Y H:i:s', strtotime("+$i days", strtotime( $datetimeout)));
$is_sunday = date('l', strtotime($date));
if($is_sunday == "Sunday")
{
$i=$i+1;
}
$day = date('l', strtotime("$i days",strtotime($datetimein)));
echo $day."<br>";
}
Use DateTime and all those objects. Simpler and cleaner :-)
<?php
$begin = new DateTime();
$end = clone $begin;
$end = $end->modify('+14 day');
$interval = new DateInterval('P1D');
$range = new DatePeriod($begin, $interval ,$end);
foreach($range as $date) {
if ($date->format('N') !== 7) {
echo $date->format('Y-m-d'), '<br>';
}
}
Date format N is the day of week as a number where 7 === Sunday.
Here is you programe
$datetimein = '01-04-2017 8:00:00';
$datetimeout= '01-04-2017 5:00:00';
$lenght = 5;
$i=0;
$days = array();
$dt = strtotime($datetimein);
while($i < $lenght){
if(date('D',$dt)!='Sun'){
$days[] = date('Y-m-d D',$dt);
$i++;
}
$dt = $dt+24*3600;
}
print_r($days);
In this line $days[] = date('Y-m-d D',$dt); change the format or save both in and out time whatever you want. $days will have you expected dates.
It looks like you have mis-spelled the variable length.
Check this.
<?php
$lenght = 5;
$in_temp = 0;
$out_temp = 0;
for($i=0;$i<=$lenght;$i++) {
$in = strtotime($datetimein) + $in_temp;
$out = strtotime($datetimeout) + $out_temp;
$date = date('m/d/Y H:i:s', strtotime("+$i days", $in));
$edate = date('m/d/Y H:i:s', strtotime("+$i days", $out));
$is_sunday = strtolower(date('l', strtotime($date))) == 'sunday';
if ($is_sunday) {
$in_temp += 86400 ; // Adding 1 day in seconds.
$day = date('l', (strtotime($date)+$in_temp));
}
else {
$day = date('l', (strtotime($date)));
}
echo $day."\n";
}
?>

PHP calculate between two time with H:i:s format

i'm trying to find and calculate between startime, finish time as: starttime + 1 hour and current time. if current time is between start and finish i must be print message such as please try after 1 hour:
$current_date_time = new DateTime("now", new DateTimeZone("Asia/Tehran"));
$user_current_time = $current_date_time->format("H:i:s");
$start_limit_time = date("H:i:s",strtotime('2015-09-15 14:57:31'));
$finish_limit_time = date('H:i:s', strtotime($start_limit_time) + (60 * 60 * 1));
$date1 = DateTime::createFromFormat('H:i:s', $user_current_time);
$date2 = DateTime::createFromFormat('H:i:s', $start_limit_time);
$date3 = DateTime::createFromFormat('H:i:s', $finish_limit_time);
if ($date1 > $date2 && $date1 < $date3)
{
echo 'here';
}
this code is not correct and i can not fix that,
You can try this, it shows the difference in minutes:
$current_date_time = new DateTime("now", new DateTimeZone("Asia/Tehran"));
$user_current_time = $current_date_time->format("H:i:s");
$start_limit_time = date("H:i:s",strtotime('2015-09-15 14:57:31'));
$finish_limit_time = date('H:i:s', strtotime($start_limit_time) + (60 * 60 * 1));
$date1 = DateTime::createFromFormat('H:i:s', $user_current_time);
$date2 = DateTime::createFromFormat('H:i:s', $start_limit_time);
$date3 = DateTime::createFromFormat('H:i:s', $finish_limit_time);
if ($date1 > $date2 && $date1 < $date3)
{
$tryAgainIn = $date3->diff( $date1 );
// just minutes
echo "try again in ".$tryAgainIn->format( "%i minutes" );
// or hours and minutes
$hours = $tryAgainIn->format('%h');
$minutes = $tryAgainIn->format('%i');
echo "try again in $hours hours and $minutes minutes";
}
For more information take a look at: DateTime::diff
At first you should avoid operating with strings format, as they should only be used IMHO to printing and retrieving data from outside. Use only timestamp or OOP methods.
I believe, that this is something you are looking for:
$startTime = new DateTime('2015-09-15 14:57:31');
$endTime = clone $startTime;
$endTime->modify('+1 hour');
if ($startTime->getTimestamp() <= time() && time() < $endTime->getTimestamp()) {
echo 'here';
}
I wonder why you need to use H:i:s format. Can you give some bigger picture?
Edit: Try this, as prior to now I did not fully understand what you want to do ;)
$origin = new DateTime('2015-09-15 14:57:31');
$startTime = new DateTime('today '.$origin->format('H:i:s'));
$endTime = clone $startTime;
$endTime->modify('+1 hour');
if ($startTime->getTimestamp() <= time() && time() < $endTime->getTimestamp()) {
echo 'here';
}

Calculate time difference between pm to am and check if it is in the current time range

Hello i want to check if the current time is between two time range and calculate the difference between them, so far i have this but its not working
$current_time = "11:14 pm";
$start_time = "11:00 pm";
$end_time = "07:55 am";
$date1 = DateTime::createFromFormat('H:i a', $current_time);
$date2 = DateTime::createFromFormat('H:i a', $start_time);
$date3 = DateTime::createFromFormat('H:i a', $end_time);
if ($date1 > $date2 && $date1 < $date3) {
echo 'in range';
} else {
echo 'not in range';
}
But it says "not in range"!
The main issue with your original code is that you are having it create dates from 3 times with unexpected results.
The start of the range is "11:00p" which it assumes means today at 11p.
The end of the range is "7:00a" which is assumes is also today. You actually intend to say "tomorrow at 7:00a".
You could try using strtotime.
$currentTime = strtotime("11:14 pm");
$rangeStart = strtotime("11:00 pm");
$rangeEnd = strtotime("tomorrow 07:55 am");
if ($currentTime >= $rangeStart && $currentTime <= $rangeEnd) {
echo 'in range';
} else {
echo 'not in range';
}
Or you could include actual dates and do something like this:
$currentTime = DateTime::createFromFormat("Y-m-d H:i:s", "2015-01-01 23:14:00");
$rangeStart = DateTime::createFromFormat("Y-m-d H:i:s", "2015-01-01 23:00:00");
$rangeEnd = DateTime::createFromFormat("Y-m-d H:i:s", "2015-01-02 07:55:00");
if ($currentTime >= $rangeStart && $currentTime <= $rangeEnd) {
echo 'in range';
} else {
echo 'not in range';
}
When start is after end you need to deal with a day change.
$current_time = "11:14 pm";
$start_time = "11:00 pm";
$end_time = "07:55 am";
$date1 = DateTime::createFromFormat('H:i a', $current_time)->getTimestamp();
$date2 = DateTime::createFromFormat('H:i a', $start_time)->getTimestamp();;
$date3 = DateTime::createFromFormat('H:i a', $end_time)->getTimestamp();
if ($date3 < $date2) {
$date3 += 24 * 3600;
if ($date1 < $date2) {
$date1 += 24 *3600;
}
}
if ($date1 > $date2 && $date1 < $date3) {
echo 'in range';
} else {
echo 'not in range';
}

Last day of each month within a date range in php

How to get last day of each month within a date range in PHP?
Input:
$startdate = '2013-01-15'
$enddate = '2013-03-15'
The output that I want is:
2013-01-31 End date of January
2013-02-28 End date of February
2013-03-15 *End date of March is '2013-03-31' but the end date is '2013-03-15' .
So I want 2013-03-15.
How can I do that?
In the future, try to write the code yourself. If you are stuck with a specific part you can request help. Exception for this one time:
<?php
$startdate = new DateTime('2013-01-15');
$enddate = new DateTime('2013-04-15');
$year = $startdate->format('Y');
$start_month = (int)$startdate->format('m');
$end_month = (int)$enddate->format('m');
for ( $i=$start_month; $i<$end_month; $i++) {
$date = new DateTime($year.'-'.$i);
echo $date->format('Y-m-t').' End date of '.$date->format('F');
}
echo $enddate->format('Y-m-d');
Will output:
2013-01-31 End date of January
2013-02-28 End date of February
2013-03-31 End date of March
2013-04-15
Note that this does not work if the start and end dates have a different year. I have left this as an exercise.
function get_months($date1, $date2) {
$time1 = strtotime($date1);
$time2 = strtotime($date2);
$my = date('mY', $time2);
$months = array(date('Y-m-t', $time1));
$f = '';
while($time1 < $time2) {
$time1 = strtotime((date('Y-m-d', $time1).' +15days'));
if(date('F', $time1) != $f) {
$f = date('F', $time1);
if(date('mY', $time1) != $my && ($time1 < $time2))
$months[] = date('Y-m-t', $time1);
}
}
$months[] = date('Y-m-d', $time2);
return $months;
}
$myDates = get_months('2005-01-20', '2005-11-25');
$myDates will have the output you want. It will work even if years are different. Logic is from this URL
I'm little changed the function:
function get_months($date1, $date2) {
$time1 = strtotime($date1);
$time2 = strtotime($date2);
$my = date('mY', $time2);
$f = '';
while($time1 < $time2) {
$time1 = strtotime((date('Y-m-d', $time1).' +15days'));
if(date('F', $time1) != $f) {
$f = date('F', $time1);
if(date('mY', $time1) != $my && ($time1 < $time2))
$months[] = array(date('Y-m-01', $time1),date('Y-m-t', $time1));
}
}
$months[] = array(date('Y-m-01', $time2),date('Y-m-d', $time2));
return $months;
}

If time and date before php

I found this snippet here.
$resttimefrom = 50000; // 5am
$resttimeto = 170000; // 5pm
$currentTime = (int) date('Gis'); // time now
if ($currentTime > $resttimefrom && $currentTime < $resttimeto)
{
// its before 5pm lets do something
}
else
{
// after 5pm sorry
}
This worked perfectly fine but I would like to limit it by date also for the site I am working on I want it to check a timestamp if delivery = tomorrow then they must cancel the order before 5pm the day before.
$timestamp = time();
$date = date("Y-m-d", $timestamp);
$time = date("G", $timestamp);
$tomorrow = date('Y-m-d', strtotime('tomorrow'));
if ($date == $tomorrow && $time > 5 && $time < 17) {
// It is in-between 5am and 5pm tomorrow.
}
if (time() >= strtotime(date('Y-m-d', time()).' 5:00:00') && time() <= strtotime(date('Y-m-d', time()).' 17:00:00')) {
// today between 5AM and 5PM
}
else {
}

Categories