PHP Date

the php date method is used to format a date and time.


PHP Date() Function

the php date() function is used to formats a timestamp to a more readable date and time.

the date() method returns a formatted date string.

a timestamp is a sequence of characters, denoting the date and time at which a certain event occurred.

syntax:

         data( format,    timestamp )
  • format : required. the format of the timestamp
  • timestamp : optional. a timestamp.

format characters that can be used:

  • d - represents the day of month, 2 digits with leading zeros (01 to 31)
  • m - numeric representation of a month , with leading zeros (01 through 12)
  • y - a two digit representation of a year (99 or 13)
  • y - a four digit representation of a year (1999 or 2013)
  • a - am or pm

look list of all format characters format characters.

note : if you do not specify a timestamp the current date and time will be used.

some other character like ':', '.', '/', '-', "," can also be used between the format characters to add additional formatting:

   <?php     //if you do not specify a timestamp the current date and time will be used 
    
                echo   date( "m.d.y" );
                
                echo   "<br/>". date( "m.d.y" );
                
                echo   "<br/>". date( "m-d-y" );
                
                echo   "<br/>". date( "m:d:y:a");
                
                echo   "<br/>". date( "h:i:s, m:d:y");   

  ?>

PHP Date() Timestamp

the php mktime() method is used to returns the unix timestamp for a date.

the unix timestamp is a long integer containing the number of seconds the unix epoch (january 1 1970 00:00:00 gmt) and the time specified.

syntax:

         mktime( hour, minute, second, month, day, year, is_dst)

this method returns the unix timestamp of the arguments given. if the arguments are invalid, returns false or -1.

in this example below format date using timestamp value:

   <?php       
             echo   date( "m.d.y", mktime(5, 42, 56, 1, 30, 2013));
                
             echo   "<br/>". date( "m.d.y", mktime(06, 12, 23, 3, 12, 2014));
                
             echo   "<br/>". date( "m-d-y", mktime(5, 42, 56, 4, 12, 2015));
            
             echo   "<br/>". date( "m:d:y:a", mktime(5, 42, 56, 5, 13, 2011));
                
             echo   "<br/>". date( "h:i:s, m:d:y", mktime(5, 42, 56, 1, 30, 2013));   

  ?>

PHP time() function

the php time() function is used to get the current time in the number of seconds since the unix epoch (january 1 1970 00:00:00 gmt).

syntax:

         time()
     <?php
                $c_time =  time();
                
                echo   "the time $c_time <br/>";
                
                echo   date( "m-d-y", $c_time);
                
                echo   "<br/>". date( "m-d-y, h-i-s", $c_time);
     ?>

PHP date timezone

the php date_default_timezone_set() function sets the default timezone used by all date/time functions in the php script.

this function returns false if the timezone is not valid or true otherwise.

syntax:

         date_default_timezone_set( timezone )

timezone :- required. specifies the timezone to use look like 'utc', 'asia/tokyo', 'europe/berlin'

list of supported timezones

   <?php           date_default_timezone_set('utc');
     
             echo   date_default_timezone_get(). "<br/>";
                       
             echo   date( "m.d.y", mktime(5, 42, 56, 1, 30, 2013));
                
             echo   "<br/>". date( "m.d.y", mktime(06, 12, 23, 3, 12, 2014));
                
             echo   "<br/>". date( "m-d-y", mktime(5, 42, 56, 4, 12, 2015));
            
             echo   "<br/>". date( "m:d:y:a", mktime(5, 42, 56, 5, 13, 2011));
                
             echo   "<br/>". date( "h:i:s, m:d:y", mktime(5, 42, 56, 1, 30, 2013));   

  ?>

the date_default_timezone_get() function returns the default timezone used by all date/time functions in the script