Skip to content

Date and Time

These functions are for working with date and time values.

dateDiffInterval

This function return the difference between two dates in days, hours, minutes or seconds.

<?php

function dateDiffInterval( $sDate1, $sDate2, $sUnit = 'H' ) { //--------------------------------------------------------------------
    //subtract $sDate2-$sDate1 and return the difference in $sUnit (Days,Hours,Minutes,Seconds)
    $nInterval = strtotime( $sDate2 ) - strtotime( $sDate1 );
    if ( $sUnit == 'D' ) { // days
        $nInterval = $nInterval / 60 / 60 / 24;
    } else if ( $sUnit == 'H' ) { // hours
        $nInterval = $nInterval / 60 / 60;
    } else if ( $sUnit == 'M' ) { // minutes
        $nInterval = $nInterval / 60;
    } else if ( $sUnit == 'S' ) { // seconds
    }
    return $nInterval;
} //end function dateDiffInterval       

?>

getCopyright

This function returns a copyright year or range.

<?php

function getCopyright( $year = 'auto' ) { //--------------------------------------------------------------------
  // based on a post by Raman Aggarwal at https://stackoverflow.com/questions/18137336/how-do-i-make-a-php-footer-dynamic-copyright-starting-this-year

  // examples:
  // auto_copyright('2010');      returns '2010-2015'
  // auto_copyright();            returns just the current year i.e: '2015'

  $currentYear = date( 'Y' );

  if ( $year === 'auto' ) {
    $year = $currentYear;
  }

  $year = intval( $year );

  if ( $year < $currentYear ) {
    $copyright = $year . ' - ' . $currentYear;
  } else {
    $copyright = $currentYear;
  }

  return $copyright;

} //end function getCopyright

?>

time_duration

This function converts a duration into English written format.

<?php

function time_duration( $seconds, $use = null, $zeros = false ) { //--------------------------------------------------------------------
    /**
     * A function for making time periods readable
     *
     * @author      Aidan Lister <aidan@php.net>
     * @version     2.0.0
     * @link        http://aidanlister.com/2004/04/making-time-periods-readable/
     * @param       int     number of seconds elapsed
     * @param       string  which time periods to display
     * @param       bool    whether to show zero time periods
     */

    // Define time periods
    $periods = array(
        'years' => 31556926,
        'Months' => 2629743,
        'weeks' => 604800,
        'days' => 86400,
        'hours' => 3600,
        'minutes' => 60,
        'seconds' => 1
    );

    // Break into periods
    foreach ( $periods as $period => $value ) {
        $seconds = ( float )$seconds;
        $value = ( float )$value;
        if ( $use && strpos( $use, $period[ 0 ] ) === false ) {
            continue;
        }
        $count = floor( $seconds / $value );
        if ( $count == 0 && !$zeros ) {
            continue;
        }
        $segments[ strtolower( $period ) ] = $count;
        $seconds = fmod( $seconds, $value );
    }

    // Build the string
    foreach ( $segments as $key => $value ) {
        $segment_name = substr( $key, 0, -1 );
        $segment = $value . ' ' . $segment_name;
        if ( $value != 1 ) {
            $segment .= 's';
        }
        $array[] = $segment;
    }

    $str = implode( ', ', $array );
    return $str;
}

?>
Back to top