alistairphillips.com

I’m : a web and mobile developer based in the Australia.


Zend PHP and String to Date

Instead of having to re-invent the wheel each time I needed to convert 'today', 'tomorrow', 'last week' into a date I put together a view helper. Using this I can now have select boxes with values of 'tomrrow', 'lastyear' etc and be able to easily process it. You receive an array back with a star / end date range.

class Zend_View_Helper_FormatStringToDate {

    /**
     * Returns a start and end date for a texual date representation ( such as tomorrow )
     *
     * @param  string $textDate  Textual version of a date, such as 'tomorrow'
     * @return false|array containing start_date and end_date
     *
     */
    public function formatStringToDate( $textDate )
    {
        if ( empty( $textDate ) ) return false;
        $textDate = strtolower( $textDate );

        $date['start'] = '';
        $date['end'] = '';

        $tmpDateTime = new DateTime();

        switch( $textDate ) {
            case 'today':
                $date['start'] = $tmpDateTime->format( 'Y-m-d' );
                $date['end'] = $date['start'];
                break;

            case 'tomorrow':
                $tmpDateTime->modify( '+1 day' );
                $date['start'] = $tmpDateTime->format( 'Y-m-d' );
                $date['end'] = $date['start'];

            case 'yesterday':
                $tmpDateTime->modify( '-1 day' );
                $date['start'] = $tmpDateTime->format( 'Y-m-d' );
                $date['end'] = $date['start'];
                break;

            case 'thisweek':
            case 'lastweek':
                if( $textDate == 'lastweek' ) {
                    $weekStart = new DateTime('-1 week');
                    $weekEnd = new DateTime('-1 week');
                }
                else{
                    $weekStart = new DateTime();
                    $weekEnd = new DateTime();
                }
                $weekdayNumber = $tmpDateTime->format('N');
                $weekStart->modify('-' . ($weekdayNumber - 1) . ' day');
                $weekEnd->modify('+' . (7 - $weekdayNumber) .' day');
                $date['start'] = $weekStart->format('Y-m-d');
                $date['end'] = $weekEnd->format('Y-m-d');
                break;

            case 'thismonth':
            case 'lastmonth':
                if( $textDate == 'lastmonth' ) {
                    $tmpDateTime->modify('-1 month');
                }
                $date['start'] = $tmpDateTime->format('Y-m-01');
                $date['end'] = $tmpDateTime->format('Y-m-t');
                break;

            case 'thisquarter':
            case 'lastquarter':
                if( $textDate == 'lastquarter') {
                    $tmpDateTime->modify('-3 months');
                }

                $monthNumber = $tmpDateTime->format('n');

                switch($monthNumber) {
                    case 1:
                    case 2:
                    case 3:
                        $date['start'] = $tmpDateTime->format('Y-01-01');
                        $date['end'] = $tmpDateTime->format('Y-03-31');
                        break;
                    case 4:
                    case 5:
                    case 6:
                        $date['start'] = $tmpDateTime->format('Y-04-01');
                        $date['end'] = $tmpDateTime->format('Y-06-30');
                        break;
                    case 7:
                    case 8:
                    case 9:
                        $date['start'] = $tmpDateTime->format('Y-07-01');
                        $date['end'] = $tmpDateTime->format('Y-09-30');
                        break;
                    case 10:
                    case 11:
                    case 12:
                        $date['start'] = $tmpDateTime->format('Y-10-01');
                        $date['end'] = $tmpDateTime->format('Y-12-31');
                        break;
                }
                break;

            case 'thisyear':
            case 'lastyear':
                if( $textDate == 'lastyear') {
                    $tmpDateTime->modify('-1 year');
                }
                $date['start'] = $tmpDateTime->format('Y-01-01');
                $date['end'] = $tmpDateTime->format('Y-12-31');
                break;
        }

        if ( !empty( $date['start'] ) && !empty( $date['end' ] ) ) {
            return $date;
        }

        return false;
    }