Programming Basics

  Home arrow Programming Basics arrow More Time Manipulation with PHP
PROGRAMMING BASICS

More Time Manipulation with PHP
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2012-02-01

    Table of Contents:
  • More Time Manipulation with PHP
  • Summary

  •  
     

    SEARCH CODEWALKERS

    More Time Manipulation with PHP


    (Page 1 of 2 )

    In this conclusion to an eight-part article series on manipulating date and time in programs with PHP, you'll learn how to use the getWeekday() method, the getDayOfYear() method, and many more. This article is excerpted from chapter 12 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).

    More Manipulation Methods

    getWeekday()

    integer getWeekday()

    ThegetWeekday()method returns the numerical offset of the day specified by the date object. An example follows:

    $date = new Date();
    $date->setDMY(30,4,2006);
    echo $date->getWeekday();

    This returns the following, which is a Sunday, because Sundayís numerical offset is 7:

    --------------------------------------------7
    --------------------------------------------

    setToWeekday()

    boolean setToWeekday (int weekday, int n [, int month [, int year]])

    ThesetToWeekday()method sets the date to thenthweekdayof themonthandyear, returningTRUEon success andFALSEotherwise. If nomonthandyearare provided, the present month and year are used. As of the time of writing, this method was broken; quite likely it will have been fixed by the time this book is published.

    getDayOfYear()

    integer getDayOfYear()

    ThegetDayOfYear()method returns the numerical offset of the day specified by the date object. An example follows:

    $date = new Date();
    $date->setDMY(4,7,1776);
    echo $date->getDayOfYear();

    The following is the result:

    --------------------------------------------186
    --------------------------------------------

    getWeekOfYear()

    integer getWeekOfYear()

    ThegetDayOfYear()method returns the numerical offset of the week specified by the date object:

    $date = new Date();
    $date->setDMY(4,7,1776);
    echo $date->getWeekOfYear();

    This returns:

    --------------------------------------------27
    --------------------------------------------

    getISOWeekOfYear()

    integer getISOWeekOfYear()

    ThegetISOWeekOfYear()method returns the week number of the date represented by the date object according to the ISO 8601 specification. ISO 8601 states that the first week of the year is the week containing the first Thursday. For instance, the first day of 2005 fell on a Sunday, but January 2 through 8 contained the first Thursday; therefore, January 1 does not even count as falling in the first week of the year. You might think this a tad odd; however, the decision is almost arbitrary in that it just standardizes the method for determining what constitutes the yearís first week. Letís see this explanation in action by querying for the week number in which January 4 falls:

    $date = new Date();
    $date->setDMY(4,1,2005);
    echo $date->getISOWeekOfYear();

    The following is returned:

    --------------------------------------------1
    --------------------------------------------

    So, given that January 1 doesnít qualify as falling within the first week of the year, within what week does it fall? You might be surprised to learn the ISO standard actually considers it to be the 53rd week of 2004:

    $date = new Date();
    $date->setDMY(1,1,2005);
    echo $date->getISOWeekOfYear();

    This returns:

    --------------------------------------------53
    --------------------------------------------

    setToLastMonthDay()

    boolean setToLastMonthDay()

    ThesetToLastMonthDay()method adjusts the date objectísday attribute to the last day of the month specified by themonth attribute, returningTRUE on success andFALSEotherwise. An example follows:

    $date = new Date();
    $date->setDMY(1,4,2006);
    $date->setToLastMonthDay();
    echo $date->getDay();

    The following output is returned:

    --------------------------------------------30
    --------------------------------------------

    setFirstDow()

    boolean setFirstDow()

    ThesetFirstDow()method sets the date objectísdayattribute to the first day of the week as specified by theweekstartattribute, returningTRUEon success andFALSEotherwise. By default,weekstartis set to Monday. The following example sets the date April 28, 2006 (which is a Friday), and then moves the date to the first day of the week (a Monday):

    $date = new Date();
    $date->setDMY(28,4,2006);
    $date->setFirstDow();
    $dcs = $date->getArray();
    print_r($dcs);

    This returns:

    --------------------------------------------Array (
    [day] => 24 [month] => 4 [year] => 2006
    [hour] => 0 [min] => 0 [sec] => 0
    )
    --------------------------------------------

    setLastDow()

    boolean setLastDow()

    ThesetLastDow()method sets the date objectísday attribute to the last day of the week, returningTRUEon success andFALSEotherwise. This day is dependent upon the value of theweekstart attribute, which is set to Monday by default. The following example sets the date April 28, 2006 (which is a Friday), and then moves the date to the last day of the week (a Sunday):

    $date = new Date();
    $date->setDMY(28,4,2006);
    $date->setLastDow();
    $dcs = $date->getArray();
    print_r($dcs);

    This returns:

    --------------------------------------------Array (
    [day] => 30 [month] => 4 [year] => 2006
    [hour] => 0 [min] => 0 [sec] => 0
    )
    ---------------------------------------

    More Programming Basics Articles
    More By Apress Publishing

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP

    Developer Shed Affiliates

     



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