Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - Working with dates and times in PHP
MISCELLANEOUS

Working with dates and times in PHP
By: Hermawan Haryanto
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-11-12

    Table of Contents:
  • Working with dates and times in PHP
  • Human Readable Formats
  • Making your own date and time
  • My own Language

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Working with dates and times in PHP - Making your own date and time


    (Page 3 of 4 )

    Are you satisfied now that you can show your user for the current date and time? If you do, then don’t need to read this article for more. The next step is for people who need something more.

    The next function that we have to understand is mktime(). This function will make your own timestamp from a given set of variables that you pass to the function.

    int mktime ( int hour, int minute, int second, 
         int month, int day, int year [, int is_dst])

    Now, I want to know in what day my birthday was on. My birth date is July 21st 1974. I’ll use the mktime function like so:

    <?php
    $mybirthdate 
    mktime(0,0,0,7,21,1974);
    print 
    date("l"$mybirthdate);
    ?>

    Wow, I finally found out that my birthday is on Sunday. :). Now the next case I want to know is what day is 25 days from now. Let’s do it this way:

    <?php
    $next25day 
    mktime(0,0,0,date("n"),date("j")+25,date("Y"));
    print 
    date("l"$next25day);
    ?>

    Give attention to the variables that I passed to the mktime function. I’ve passed date("j")+25. It means that I want to add 25 to the current date. You can figure out what it will produce.

    Another method

    This is another method to access the current date or the given timestamp that you’ve produced by your own script. The method, or you can call it function, is getdate(). This function will produce an associative array containing the date information. The array key is:

    "seconds" - seconds 
    "minutes" - minutes 
    "hours" - hours 
    "mday" - day of the month 
    "wday" - day of the week, numeric: from 0 as Sunday up to 6 as Saturday 
    "mon" - month, numeric 
    "year" - year, numeric 
    "yday" - day of the year, numeric; i.e. "299"
    "weekday" - day of the week, textual, full; i.e. "Friday" 
    "month" - month, textual, full; i.e. "January"

    Example Usage:

    <?php
    $today 
    getdate();
    print 
    $today["month"// it will print the month of current date
    print $today["weekday"// it will print the day of current date
    ?>

    More Miscellaneous Articles
    More By Hermawan Haryanto

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap