PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon May 28, 2018 4:51 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: A generic date formater
PostPosted: Sun Nov 30, 2003 1:35 pm 
Offline
Forum Contributor
User avatar

Joined: Mon Mar 24, 2003 11:31 am
Posts: 150
Location: UK
Ok, over the years I have been getting frustrated at all the different date formats, so I have just finished writing the following function...

It aims to allow me to use the same date formatting routine for the various date formates:
d/m/Y
m/d/Y
Y-m-d

(also supports hours, mins and seconds)

Syntax: [ Download ] [ Hide ]
<?php

function format_date($date, $ret_fomat="Y-m-d", $dateformat="d/m/Y") {



    // Ok, first split the format

    $ArrFormat = split('[^dmYyHis]+', $dateformat);

    $ArrValues = split('[^0-9]+', $date);



    // Sort out variables

    for( $x=0; $x < count($ArrFormat); $x++ ) {



        $DateElements[$ArrFormat[$x]] = $ArrValues[$x];



    }



    // Check for short Year

    if ($DateElements[y] && !$DateElements[Y]) {

        // Christ I hate Y2k issues

        if ( $DateElements[y] < 69 )

            $DateElements[Y] = '20'.$DateElements[y];

        else

            $DateElements[Y] = '19'.$DateElements[y];

    }



    return date($ret_fomat, mktime(

        $DateElements[H],

        $DateElements[i],

        $DateElements[s],

        $DateElements[m],

        $DateElements[d],

        $DateElements[Y]

    ));

}

?>


Now I'd love to detect the 12 hour clock, but can't see a way of really doing it?

This script may or may not be useful to you, but comments would be great

Al,


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 30, 2003 1:56 pm 
Offline
DevNet Master
User avatar

Joined: Thu Aug 15, 2002 5:53 am
Posts: 4235
Location: Sussex, UK
I actually use PHP's in-built functions [php_man]strtotime[/php_man] and [php_man]date[/php_man] for date/time conversions.

Personally, I thought it was a oversight ever since PHP got bundled with MySQL to not include a function into PHP which converts MySQL timestamps.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 30, 2003 2:06 pm 
Offline
Forum Contributor
User avatar

Joined: Mon Mar 24, 2003 11:31 am
Posts: 150
Location: UK
True but [php_man]strtotime[/php_man] can't tell the difference between american (m/d/y) and british (d/m/y) time formats.

My personal favourite format is Ymd (kinda MYSQL format) as you can do quick and simple date comparisons ie:
Syntax: [ Download ] [ Hide ]
<?php

$date = '20041006';



if ($date == date('Ymd')) {

    print "It's today";

} elseif ($date < date("Ymd")) {

    print "It's historical";

} else {

    print "It's in the future";

}

?>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group