PHPBuilder - Unix timestamp to Readable date (and back)



RSS Twitter
Snippets Calendars Dates

Unix timestamp to Readable date (and back)

by: Kenley Capps
|
July 5, 2002

Version: 1.0

Type: Function

Category: Calendars/Dates

License: GNU General Public License

Description: These 2 simple functions convert unix timestamp to readable date and back. Inludes full documentation of functions.



<?

function makedate() { //start the function
    date("F j Y h:i A", time()); //please refer to php.net's documentation for more information on date function
    /* Now to explain the date() function. Here's the list of current formats
       F - Full month name. IE: January
       j - Day of month, number format. IE: 1
       Y - Full year. IE: 2002
       h - Current hour in 12 hour format. IE: 8
       : - Colon for time format. IE: :
       i - Current minute. IE: 30
       A - AM/PM Format. IE: PM
       Very simple. time() is just the unix time. And there you have it.
    */
} //end makedate()

function unixdate($input) { //start function
    $dateparts = explode(" ",$input);
    /* Take apart the date at every space. So, January 1 2002 8:40 PM, would split into 5 different strings, each called
    $dateparts[0] = January
    $dateparts[1] = 1
    $dateparts[2] = 2002
    $dateparts[3] = 8:40
    $dateparts[4] = PM
    So we've got the 5 strings. Now we need to put split one of these new strings up as well. */
    $timeparts = explode(":",$dateparts[3]);
    /* Ok, we took our newly generated string for the time ($dateparts[3]) and we split it up around the : to get two strings, the hour
    and the minute. 
    $timeparts[0] = 8
    $timeparts[1] = 40
    note that the : is now gone. it is split AROUND, and therefore is not included within the strings. */
    list($month,$day,$year,$hour,$minute,$ampm) = array($dateparts[0],$dateparts[1],$dateparts[2],$timeparts[0],$timeparts[1],$dateparts[4]); //list it for easy use
    if($ampm == "PM") $hour += 12;
    /* This part is simple. It needs to convert this into 24 hour format. So, if it's PM (if($ampm == "PM")), it'll add hours until its in 24 hours format '$hour += 12;'*/
    switch($month) { //start off a switch to decide which month goes to which numerical value
        case "January": $month = 1; break; //January = 1
        case "February": $month = 2; break; //February = 2
        case "March": $month = 3; break; //Marh = 3
        case "April": $month = 4; break; //April = 4
        case "May": $month = 5; break; //May = 5
        case "June": $month = 6; break; //June = 6
        case "July": $month = 7; break; //July = 7
        case "August": $month = 8; break; //August = 8
        case "September": $month = 9; break; //September = 9
        case "October": $month = 10; break; //October = 10
        case "November": $month = 11; break; //November = 11
        case "December": $month = 12; break; //December = 12
    } //end switch
    $input = mktime($hour,$min,$sec,$month,$day,$year); //Consult php.net documentation for function mktime for more info
    /* puts $input into standard unix timestamp with the newly made variables */
    return $input; //return it
} //end function

?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Kenley Capps

Comment:



Comment:

(Maximum characters: 1200). You have characters left.