• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives
  • Sources
  • Search

Search

strtotime

Run Code At Specific Times

January 20, 2015

If you need to run some code at certain times of the week, a weekly sale for example, PHP gives you that flexibility. In the example below, we will display an image on our website every Wednesday afternoon between 1pm and 5pm to display a sale.

Let’s create a new function called wednesday_afternoon(). We can use this function to check the date and time. The function checks for the current time and compares it if it’s Wednesday afternoon. It returns a true if it is, and a false if it falls outside those times.

function wednesday_afternoon() {
  date_default_timezone_set('America/Los_Angeles');
  $start_time = strtotime('Wednesday 1pm');
  $stop_time = strtotime('Wednesday 5pm');
  $current_time = strtotime('now');
  // return true if between start and stop times
  if ( $current_time > $start_time && $current_time < $stop_time ) {
    return true;
  }
}

A simple if statement decides what image to display based if it's Wednesday afternoon.

if (wednesday_afternoon()) {
  
} else {
}

I have posted a similar code GitHubGist.

Filed Under: PHP Tagged With: strtotime, time

  • Home
  • About
  • Archives
  • Sources
  • Search

Copyright © 2023