• Skip to primary navigation
  • Skip to main content
  • Skip to footer
  • Store
  • Support
  • My Account
  • Cart

9seeds

Building Custom WordPress Solutions | Plugin Development

 
  • Custom Development
  • Themes
  • Plugins
  • About
  • Contact
  • Blog

php

Pretty PHP Date Ranges

Posted on December 28, 2013

Pretty PHP Date Ranges

Last week I needed to come up with a way to display a pretty date range based on a starting and ending date in PHP. It’s simple enough use PHP’s date function to convert a unix timestamp into a pretty date format, but PHP doesn’t have a built in function to format date ranges.

The goal was to take two unix time stamps and output a range that looks like this:

January 25 – 26th, 2014

This sounds and is simple until you take into account that sometimes it crosses months and needs to look like this:

January 25 – February 2nd, 2014

or could cross years like this:

December 25th, 2013 – January 3rd, 2014

So here is the code to build those prettified date ranges:


 * @since 1.0
 */

function jb_verbose_date_range($start_date = '',$end_date = '') {

    $date_range = '';

    // If only one date, or dates are the same set to FULL verbose date
    if ( empty($start_date) || empty($end_date) || ( date('FjY',$start_date) == date('FjY',$end_date) ) ) { // FjY == accounts for same day, different time
        $start_date_pretty = date( 'F jS, Y', $start_date );
        $end_date_pretty = date( 'F jS, Y', $end_date );
    } else {
         // Setup basic dates
        $start_date_pretty = date( 'F j', $start_date );
        $end_date_pretty = date( 'jS, Y', $end_date );
        // If years differ add suffix and year to start_date
        if ( date('Y',$start_date) != date('Y',$end_date) ) {
            $start_date_pretty .= date( 'S, Y', $start_date );
        }

        // If months differ add suffix and year to end_date
        if ( date('F',$start_date) != date('F',$end_date) ) {
            $end_date_pretty = date( 'F ', $end_date) . $end_date_pretty;
        }
    }

    // build date_range return string
    if( ! empty( $start_date ) ) {
          $date_range .= $start_date_pretty;
    }

    // check if there is an end date and append if not identical
    if( ! empty( $end_date ) ) {
        if( $end_date_pretty != $start_date_pretty ) {
              $date_range .= ' - ' . $end_date_pretty;
          }
     }
    return $date_range;
}

Also available on GitHub/Gist here.

To actually use that in WordPress you’d just add something like this in your template file where you wanted to output the date, here based on two custom field values.

';
     echo jb_verbose_date_range( $start_date , $end_date );
     echo '';
}
Continue Reading

Jon Brown

    More by Jon Brown

    9seeds in Boston

    Posted on January 11, 2010

    Well, it’s that time again…9seeds will be in Boston for WordCamp Boston 2010 on January 23rd!

    As you all know, we love attending WordCamps. It’s a great way to meet new people, from end users to other developers and everyone in between. We will also be sponsoring this event as well as having Shayne there presenting. Shayne’s topic will be “WordPress, PHP and CSS, Oh My!”, where he will explain that without formal training and education, you can edit and modify your own WordPress theme and/or plugins. Shayne is self-taught and is going to show how he learned how to do what it is he does…and show everyone else how to as well!

    As we mentioned, we’re sponsoring the event…but it doesn’t stop there. We have a few surprises up our sleeve, but you’ll have to attend to find out what they are and at this point, the event is completely sold out! So we hope you have your tickets ready, because this is shaping up to be a incredible WordCamp and good times and lots of learning to be had by all.

    All of the sessions will be recorded and placed on WordPress.tv for later viewing in case you can’t make it, or you can follow along on Twitter with the hashtag #wcbos

    Continue Reading

    shayne

      More by shayne

      Footer

      Get in Touch

      • New Project Inquiry
      • Product Support and General Inquiry
      • Store Purchase Terms and Conditions
      • Store FAQ
      • Cookie Policy
      • Privacy Policy

      Our Services

      • Custom WP Development
      • Theme Store
      • Plugin Store

      WordPress Plugins for Sale

      • Time Tracker
      • Authorize.net SIM Gateway

      WordPress Plugins for Free

      • Simple Calendar
      • WP Chargify
      • Facebook
      • Twitter
      • LinkedIn
      • WordPress
      • GitHub

      Copyright 2021 | 9seeds, LLC