get_weekstartend

Get the week start and end from the datetime or date string from MySQL.

Description

(array) get_weekstartend( (string) $mysqlstring, (string) $start_of_week = '' ); 

Returns (array)

Keys are 'start' and 'end'.

Parameters (2)

0. $mysqlstring (string)
Date or datetime field type from MySQL.
1. $start_of_week — Optional. (string) => ''
Start of the week as an integer. Default empty string.

Usage

  1. if ( !function_exists( 'get_weekstartend' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // Date or datetime field type from MySQL. 
  5. $mysqlstring = ''; 
  6.  
  7. // Optional. Start of the week as an integer. Default empty string. 
  8. $start_of_week = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_weekstartend($mysqlstring, $start_of_week); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function get_weekstartend( $mysqlstring, $start_of_week = '' ) { 
  2. // MySQL string year. 
  3. $my = substr( $mysqlstring, 0, 4 ); 
  4.  
  5. // MySQL string month. 
  6. $mm = substr( $mysqlstring, 8, 2 ); 
  7.  
  8. // MySQL string day. 
  9. $md = substr( $mysqlstring, 5, 2 ); 
  10.  
  11. // The timestamp for MySQL string day. 
  12. $day = mktime( 0, 0, 0, $md, $mm, $my ); 
  13.  
  14. // The day of the week from the timestamp. 
  15. $weekday = date( 'w', $day ); 
  16.  
  17. if ( !is_numeric($start_of_week) ) 
  18. $start_of_week = get_option( 'start_of_week' ); 
  19.  
  20. if ( $weekday < $start_of_week ) 
  21. $weekday += 7; 
  22.  
  23. // The most recent week start day on or before $day. 
  24. $start = $day - DAY_IN_SECONDS * ( $weekday - $start_of_week ); 
  25.  
  26. // $start + 1 week - 1 second. 
  27. $end = $start + WEEK_IN_SECONDS - 1; 
  28. return compact( 'start', 'end' );