Search code examples
javascriptphpjqueryajaxget

How to use different variables at specific times in PHP


I have some PHP code..

<?php
  $artist = $_GET['artist'];
  $title = $_GET['title'];

  $r = fopen("temp_title.txt", "w");
  fwrite($r, $artist." <b>|</b> ".$title);
  fclose($r);
?>

and I would like to add another fwrite with a different variable, but it will be displayed for a certain period of time, from 11pm to 7am. Like this:

<?php
  $artist = $_GET['artist'];
  $title = $_GET['title'];
  $info = "Night Mode";

  $r = fopen("temp_title.txt", "w");
  fwrite($r, $artist." <b>|</b> ".$title);
  fwrite($r, $info); //This will be shown only in time of 11pm - 7pm
  fclose($r);
?>

and after that time, the previous fwrite should be displayed again..


Solution

  • You can just use date to get the current hour of the day and use that to control the data that is written to the file:

    $artist = $_GET['artist'];
    $title = $_GET['title'];
    $r = fopen("temp_title.txt", "w");
    $hour = date('G');
    if ($hour < 7 || $hour >= 23) {
        fwrite($r, "Night Mode"); //This will be shown only in time of 11pm - 7pm
    }
    else {
         fwrite($r, $artist." <b>|</b> ".$title);
    }
    fclose($r);