Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > PHP Forum & Tutorial


PHP Date()


Reply
Views: 2276  
Thread Tools Rate Thread
  #1  
Old 05-04-2009, 05:07 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,566
Default PHP Date()

The PHP date() function is used to format a time or a date.
The PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.
Syntax

date(format,timestamp)
Parameter Description format Required. Specifies the format of the timestamp timestamp Optional. Specifies a timestamp. Default is the current date and time (as a timestamp)

Reply With Quote
  #2  
Old 05-04-2009, 05:07 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,566
PHP Date - What is a Timestamp?

A timestamp is the number of seconds since January 1, 1970 at 00:00:00 GMT. This is also known as the Unix Timestamp.
PHP Date - Format the Date

The first parameter in the date() function specifies how to format the date/time. It uses letters to represent date and time formats. Here are some of the letters that can be used:
  • d - The day of the month (01-31)
  • m - The current month, as a number (01-12)
  • Y - The current year in four digits
An overview of all the letters that can be used in the format parameter, can be found in our PHP Date reference.


Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting:




echo date("Y/m/d");
echo "
";
echo date("Y.m.d");
echo "
";
echo date("Y-m-d");
?>



The output of the code above could be something like this:




2006/07/11
2006.07.11
2006-07-11



PHP Date - Adding a Timestamp

The second parameter in the date() function specifies a timestamp. This parameter is optional. If you do not supply a timestamp, the current time will be used.


In our next example we will use the mktime() function to create a timestamp for tomorrow.


The mktime() function returns the Unix timestamp for a specified date.
Reply With Quote
  #3  
Old 05-04-2009, 05:08 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,566
Syntax



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



To go one day in the future we simply add one to the day argument of mktime():




$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
?>



The output of the code above could be something like this:




Tomorrow is 2006/07/12
Reply With Quote
Reply

New topics in PHP Forum & Tutorial





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)