Determine time in function

Asked

Viewed 297 times

2

<?php $ler = $_COOKIE['viu']; if($ler == "1") :?>
<?php include 'libera.php';?>



<?php else :?>

<?php include 'bloqueia.php';?><?php endif; ?>

I want to modify the above code as follows:

I want to work with schedules. If ler == 1 or it’s from 00:00 to 11:59 then it goes to the libera.php.

But if it’s 12:00 to 11:59 and reading is not 1, then it goes to the bloqueia.php.

  • failed to post the code

  • Now it worked out rsrs

2 answers

2

You can do it this way:

<?php

$hora = date("H:i:s");
$ler = $_COOKIE['viu'];

  if( $ler == 1 || (($hora > "00:00:00") && ($hora < "11:59:59")) )
    redirect('libera.php');
  else
    redirect('bloqueia.php');

?>

1


You can use the function date to get the current time.

Just remember that this function - despite the name - returns a string. A another answer suggests to return the time in the format "hh:mm:ss" and compare with the string "00:00:00". This only "works" by coincidence, since comparison of strings takes into account the lexicographical order of the characters. The detail is that the condition should be ($hora >= "00:00:00") && ($hora <= "11:59:59") (using >= and <= instead of > and <), otherwise both midnight and 11:59:59 will be ignored.

But if you think about it, to check if the time is between 00:00 and 11:59, you just need to check the value of the hour (whatever makes the minutes and seconds):

$hora = date('H');
if ($ler == 1 || ($hora >= 0 && $hora < 12)) {
    // horário entre 00:00 e 11:59
}

date('H') returns a string with the value of the time between "00" and "23" (see in documentation). However, when comparing $hora >= 0, the PHP converts the value of $hora to number. Of course, if you want, you can also explicitly convert the value to a number, using intval:

$hora = intval(date('H'));

Therefore, if the time value is greater than or equal to zero and less than 12, I know the time is between 00:00 and 11:59 (it makes the value of minutes and seconds, nor need to check them).

In fact, as the documentation ensures that the value will always be between zero and 23, just test if it is less than 12.


Time zone

Just remembering that date will take the value of the current time using the Timezone that is set in PHP. Ex:

date_default_timezone_set('Europe/London');
echo date('H'); // 18

date_default_timezone_set('Asia/Tokyo');
echo date('H'); // 03

According to the Timezone set, the value of the current time changes (in London it is now 18h, while in Tokyo it is 3am). That is, it will be considered the time that is configured on the server where PHP is running, regardless of where the user is accessing your site.

  • $hour = date('H'); if ($read == 1 || ($hour >= 18 && $hour < 0)) { // time from 18:00 to 23:59 } Ta right this way I sent up tbm if I want you to check in the time from 18hrs to 23:59?

  • @Rafaelmacedo Think about it this way: if the time is greater than 18, then it is certainly not less than zero, that is, it will never enter this if (I mean, you’ll get in if $ler == 1, but the part of the time will always be false). Just do $hora >= 18 - or, if you want to be more explicit, $hora >= 18 && $hora <= 23 (although it is redundant, since date('H') always returns values between 0 and 23, so you wouldn’t need to test for less than 23)

  • Last time I disturb you, but check this here ta see if my logica ta certain: http://prntscr.com/mxkp62 I want to read == 1 or time is greater than 18hrs, then open premium.php, otherwise, then open Bloq.php. That’s the way I did it in the print?

  • @Rafaelmacedo Yes, that’s right! :-)

  • Dude, and in your example you did, you put ($hour >= 0 && $hour < 12) for the midnight break until noon. But according to your logic that you spoke there last, I can use so tbm? Since if it is less than 12, it will automatically be greater than 0. $hora = date('H'); if ($read == 1 || $hour < 12) { // time between 00:00 and 11:59 }

  • @Rafaelmacedo Yes, as I said in the reply, it would be enough to test if he is less than 12 :)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.