-2

i have the following code :

$now = date('2018-12-28 23:00:00');
$begintime = new DateTime('22:00');
$endtime = new DateTime('03:00');

if($now >= $endtime && $now <= $begintime){
// not between times
echo "Run";
} else {
// between times
echo "Skip";
}

and the echo is

Skip

because $now is bigger than $begintime that make the output is false
what is the correct way to know if time is not the between the $begintime and $endtime?
I searched all of the relevant issues and I've just wasted 3 days of my life because of this issues, but couldn't find something that even elucidated anything from this forum and google for me. Please help me this issue has already taken an absurd amount of days from my life already and sorry for my english before.. :D

Reza Fahmi
  • 270
  • 4
  • 11
  • 1
    You are trying to compare a string (output from `date()`) and an object – Nick Dec 28 '18 at 12:56
  • You have to ''play'' with date not only time, something like that : if endtime < begintime then endtime = day +1 – Daniel E. Dec 28 '18 at 12:57
  • 1
    Possible duplicate of [PHP compare time](https://stackoverflow.com/questions/6158726/php-compare-time) – digijay Dec 28 '18 at 12:57
  • You can take a look here https://stackoverflow.com/questions/28809299/calculate-time-difference-between-pm-to-am-and-check-if-it-is-in-the-current-tim/28809498#28809498 – Ôrel Dec 28 '18 at 12:58
  • @Nick string from `date()` is a variable from my mysqli DATETIME – Reza Fahmi Dec 28 '18 at 13:20
  • @RezaFahmi agreed but it's a string and `$begintime` and `$endtime` are both `DateTime` objects so you can't compare them directly. – Nick Dec 28 '18 at 13:22
  • @Ôrel yes, i have been read it and still cant solve my problem.. – Reza Fahmi Dec 28 '18 at 13:24
  • @Nick with what i have to change it? i dont know php too much.. :( – Reza Fahmi Dec 28 '18 at 13:26
  • You say [*"date() is a variable from my mysqli DATETIME"*](https://stackoverflow.com/questions/53958916/php-check-if-time-between-2200-and-0200#comment94756014_53958916) - Why don't you just do this in your query instead? It'll be a lot simpler. Any special reason why you want to do this with PHP? – Funk Forty Niner Dec 28 '18 at 13:37
  • @FunkFortyNiner yea, iam forget, sorry.. i mean, i want to make it run between 22:00 and 03:00, because i want it sleep in time between 03:00 till 22:00.. but when i try it, it say false because `$now <= $begintime` – Reza Fahmi Dec 28 '18 at 13:44
  • hallo @FunkFortyNiner can u help me fix my question? – Reza Fahmi Dec 29 '18 at 11:03
  • Seems you came up with a solution. Why not just remove that from your question and post it as an answer? Stack does let you do that. As for asking to remove the possible duplicate, you're going to have to ask the person(s) who placed it to remove it, or flag your question for moderators. – Funk Forty Niner Dec 29 '18 at 13:12

3 Answers3

1

Your code displays enormous amount of misunderstanding of how things are working and it leads to the problem. Please take a look at official documentation, you will see that date() returns string and DateTime is an object. They can't be compared directly, you need to convert them into comparable types beforehand. Also notice that DateTime expects to get a date, not just time. Actually without date being defined your $endtime is actually smaller then $starttime:

$begintime = new DateTime('22:00');
$endtime = new DateTime('03:00');

echo $begintime < $endtime ? 'Yes':'No';

This code snippet will return No.

You need to convert $now to a DateTime and you need to add dates to your start / end time marks. For example:

$now = \DateTime::createFromFormat('Y-m-d H:i:s', '2018-12-28 23:00:00', new \DateTimeZone('UTC'));

I can't provide example of converting start / end time marks because you have not defined how do they actually need to look like.

Flying
  • 4,422
  • 2
  • 17
  • 25
  • thank you for help.. i build website which is user can set the begintime and endtime.. if user set begintime 03:00 and endtime 22:00, the script will run, but if time is 23:00, the script will not run, because the `$now` is bigger than `$begintime` – Reza Fahmi Dec 28 '18 at 13:53
0

One solution may be :

$now = "23:00"; // date ('H:i');
$begintime = "22:00";
$endtime = "03:00";

$now_t = DateTime::createFromFormat('H:i', $now);
$begintime_t = DateTime::createFromFormat('H:i', $begintime);
$endtime_t = DateTime::createFromFormat('H:i', $endtime);

if ($now_t > $begintime_t || $now_t < $endtime_t)
{
 echo "Skip";
}
else
{
 echo "Run";
}
Soren
  • 260
  • 4
  • 13
0

UPDATE

finnaly, i solve my problem..
can someone remove the duplicate tags from my question and find the correct title for this issue? maybe someone who have same issue like me, can search my question and find the answer..
sorry for my english, i still learning..

// this is variable from mysql database
$mysql_start = "2018-12-28 21:45:00";  // its from mysql DATETIME and time show when this script will be run
$begintime = "22:00"; // user can choose when this script stop
$endtime = "20:00"; // user can choose when this script run


$mysql_start = explode(' ', $mysql_start);
$taskdays = $mysql_start[0]; // 2018-12-28
echo $taskhours = $mysql_start[1]; // 21:45:00
echo "<br>";
$taskhours = explode(':',$taskhours);
$taskhours = $taskhours[0]; // 22
echo $begintime = date($begintime);
echo "<br>";
$begintime = explode(':',$begintime);
$begintime = $begintime[0]; // 20
echo "<br>";
echo $endtime = date($endtime);
echo "<br>";
$endtime = explode(':',$endtime);
$endtime = $endtime[0] - 1; // because if endtime is 6, so if its 05:59, the endtime will be end on 05
echo $endtime = str_pad($endtime, 2, '0', STR_PAD_LEFT); // if 6, it will add 0 so it will be 06
echo "<br>";
$jamarray = array("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23");
$ray = array();
if ($begintime > $endtime){
    echo '$begintime is bigger than $endtime<br>';
foreach($jamarray as $ray) {
        if($ray >= $begintime or $ray <= $endtime){
            //echo '<br>';
            //print_r($ray);
            $eray[] = $ray;
        }
}
$aslinya = array_diff($jamarray,$eray); 
print_r($aslinya);
if (in_array($taskhours, $aslinya))
  {
  echo " <= script run in this time";

  }

}else{

    echo '$begintime is less than $endtime<br>';
foreach($jamarray as $ray) {
        if($ray >= $begintime and $ray <= $endtime){
            //echo '<br>';
            //print_r($ray);
            $eray[] = $ray;
        }
}
$aslinya = array_diff($jamarray,$eray);
print_r($aslinya);
if (in_array($taskhours, $aslinya))
  {
  echo " <= script run in this time";
  } 


}
Reza Fahmi
  • 270
  • 4
  • 11