Skip to Content
Menu

relative_time()

Get the current relative time of day.

PHP April 16, 2025

Usage

PHP
nebula()->relative_time($format)

Parameters

$format
(Optional) (String) The format of requested data.
Default: None

Parameter Notes

Possible formats include:

  • “description”: An array of “early” or “late” and “morning”, “afternoon”, “evening”, and “night”.
  • “standard”: An array of the three hour range in 12-hour time.
  • “military”: An array of the three hour range in 24-hour time.
  • “ampm”: A string of “am” or “pm”.

Request or provide clarification »

Additional Notes

This is used for more general time-of-day functions. The breakdown is as follows:

  • Early Morning: 6am, 7am, 8am
  • Late Morning: 9am, 10am, 11am
  • Early Afternoon: 12pm, 1pm, 2pm
  • Late Afternoon: 3pm, 4pm, 5pm
  • Early Evening: 6pm, 7pm, 8pm
  • Late Evening: 9pm, 10pm, 11pm
  • Early Night: 12am, 1am, 2am
  • Late Night: 3am, 4am, 5am

Note: Thinking of changing the name of this function to something like “nebula_general_time()” or “nebula_time_of_day()”.

Similarly, here is a graphic of body classes that get added for various times of day. This also includes the relative times used in this function”

Getting the difference between two timestamps

For reference, if you need to get the difference between two timestamps, use the WordPress function human_time_diff() which accepts a timestamp like time() and returns a string such as “5 hours”. https://developer.wordpress.org/reference/functions/human_time_diff/

 

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Functions.php on line 2307.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_nebula_relative_time"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    PHP
            public function relative_time($format=null){
                $override = apply_filters('pre_nebula_relative_time', null, $format);
                if ( isset($override) ){return $override;}
    
                if ( $this->contains(date('H'), array('00', '01', '02')) ){
                    $relative_time = array(
                        'description' => array('early', 'night'),
                        'standard' => array(0, 1, 2),
                        'military' => array(0, 1, 2),
                        'ampm' => 'am'
                    );
                } elseif ( $this->contains(date('H'), array('03', '04', '05')) ){
                    $relative_time = array(
                        'description' => array('late', 'night'),
                        'standard' => array(3, 4, 5),
                        'military' => array(3, 4, 5),
                        'ampm' => 'am'
                    );
                } elseif ( $this->contains(date('H'), array('06', '07', '08')) ){
                    $relative_time = array(
                        'description' => array('early', 'morning'),
                        'standard' => array(6, 7, 8),
                        'military' => array(6, 7, 8),
                        'ampm' => 'am'
                    );
                } elseif ( $this->contains(date('H'), array('09', '10', '11')) ){
                    $relative_time = array(
                        'description' => array('late', 'morning'),
                        'standard' => array(9, 10, 11),
                        'military' => array(9, 10, 11),
                        'ampm' => 'am'
                    );
                } elseif ( $this->contains(date('H'), array('12', '13', '14')) ){
                    $relative_time = array(
                        'description' => array('early', 'afternoon'),
                        'standard' => array(12, 1, 2),
                        'military' => array(12, 13, 14),
                        'ampm' => 'pm'
                    );
                } elseif ( $this->contains(date('H'), array('15', '16', '17')) ){
                    $relative_time = array(
                        'description' => array('late', 'afternoon'),
                        'standard' => array(3, 4, 5),
                        'military' => array(15, 16, 17),
                        'ampm' => 'pm'
                    );
                } elseif ( $this->contains(date('H'), array('18', '19', '20')) ){
                    $relative_time = array(
                        'description' => array('early', 'evening'),
                        'standard' => array(6, 7, 8),
                        'military' => array(18, 19, 20),
                        'ampm' => 'pm'
                    );
                } elseif ( $this->contains(date('H'), array('21', '22', '23')) ){
                    $relative_time = array(
                        'description' => array('late', 'evening'),
                        'standard' => array(9, 10, 11),
                        'military' => array(21, 22, 23),
                        'ampm' => 'pm'
                    );
                }
    
                if ( !empty($format) ){
                    return $relative_time[$format];
                } else {
                    return $relative_time;
                }
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_nebula_relative_time', 'my_custom_relative_time', 10, 2); //The last integer must be 1 more than the actual parameters
    function my_custom_relative_time($null, $format){ //$null is required, but can be ignored
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_nebula_relative_time', '__return_false');