PHP Calendar and Date functions Functions

These functions are used to get dates and times, as well as information about calendars

  • getdate()

    • getdate returns an associative array with date and time information
    • array getdate([int $time])
    • Parameters:
      • int $time: Optional. Default the current time. You can set $time with mktime
      Return Values:
      • Returns an associative array with the following attributes:
        • seconds
        • minutes
        • hours
        • mday(day of the month)
        • wday(day of the week numerically)
        • mon(numeric month)
        • year
        • weekday(textual day of week)
        • month(textual month)
        • 0(Seconds since January 1st, 1970, the start of the Unix Epoch)
      Examples:
      <?php
      	$date=getdate();
      	echo "The time is $date[hours]:$date[minutes]:$date[seconds], $date[weekday], $date[month] $date[mday], $date[year]";
      	
      	//Returns
      
      	The time is 9:27:19, Thursday, July 18, 2013	
      				
      ?>
  • date

    • date takes a string format and a timestamp, and returns a formatted string
    • string date(string $format, [int $time])
    • Parameters: Return Values:
      • Returns a string formatted as specified by $format
      Examples:
      <?php
      	echo $formatted=date("h:i:s: A, D d, F, Y"); //Hours(leading zero):minutes(leading zeros):seconds(leading zeros), AM/PM, Day of week, Day of month, Month, year
      	
      	//Returns:
      	
      	 09:27:19: AM, Thu, 18, July, 2013
      ?>
  • mktime

    • mktime takes a time and returns a unix timestamp
    • int mktime([int $hour], [int $minute], [int $second], [int $month], [int $day], [int $year], [int $daylight_savings])
    • Parameters:
      • int $hour: Optional. Default the current hour. The number of hours since the start of the day
      • int $minute: Optional. Default the current minute. The number of minutes since the start of the hour.
      • int $second: Optional. Default the current second. The number of seconds since the start of the minute
      • int $month: Optional. Default the current month.
      • int $day: Optional. The day of week
      • int $year: Optional. The year
      • int $daylight_savings: Optional. If daylight savings time is on
      Return values:
      • Returns the Unix Timestamp
      Examples:
      <?php
      	echo $time=date("h:i:s: A, D, d, F, Y", mktime(9,14,54,7,14,2013));
      	
      	//Returns
      	
      	09:14:54: AM, Sun, 14, July, 2013
      ?>
  • cal_days_in_month

    • cal_days_in_month returns the number of days in a month, given a calendar, month, and year
    • int cal_days_in_month(int $calendar_type, int $month, int $year)
    • Parameters:
      • int $calendar_type: Either CAL_GREGORIAN(recommended), or CAL_JULIAN
      • int $month: Numerical representation of month. 1 is January, etc
      • int $year: 4 digit year
      Return Values:
      • returns an integer number of days in the month
      <?php
      	echo cal_days_in_month(CAL_GREGORIAN,2,2012); //Leap year
      	
      	//Returns:
      	
      	29	
      ?>