Php – Date and Time

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/php-date-time/
PINTEREST
PINTEREST
INSTAGRAM

PHP Tutorial Topics

  1. Introduction To PHP
  2. Installing PHP To Linux
  3. Installing PHP To Windows
  4. PHP Hello World Project with Eclipse
  5. PHP Syntax
  6. PHP Variables and Types
  7. PHP String Types
  8. PHP Numeric Type (Integer, Float, Double)
  9. PHP Boolean Type
  10. PHP Arrays
  11. PHP Operators
  12. PHP Ternary Operators
  13. PHP Constants
  14. PHP If..Elseif..Else Conditional Statements
  15. PHP Switch..Case Conditional Statements
  16. PHP For and Foreach Loop Statements
  17. PHP While and Do While Statements
  18. PHP Functions
  19. PHP Class and Objects
  20. PHP Date and Time Operations

Php provides very enhanced date and time functions. There are a lot of different use cases for date and time functions. We will try look most of them in this tutorial.

Date Function

date() function is used to get date in different formats. date() function can be used to get time stamp too. Unix Timestamp is the second count from January 1, 1970 . The syntax of the date function is like below. Here TIMESTAMP is optional so if we want to get current date we do no need to provide TIMESTAMP.

In this example we will get the current date as day-month-year format.

This will print following date.

Formatting

Php provides rich set of printing options. In this part we will look formatting options. Here the list.

  •  d represents day in 01 and 31 format
  • D represents day in Mon to Sun format
  • m represents month in 01 to 12 format
  • M represents month abbreviated Jan to Dec format
  • y represents year in two digits from 08 to 14 format
  • Y represents year in four digits 2008 or 2017 format
  • h represents hour in 12-hour format from01 to 12
  • H represents hour in 24-hour format from 00 to 24
  • i represents minutes in two digits 00 to 59
  • s represents seconds in two digits 00 to 59
  • a represents lowercase ante meridiem and post meridiem am or pm
  • A represents lowercase ante meridiem and post meridiem AM or PM

Time Function

Another useful function provided by Php is time() . Time function is used to get current time Unix timestamp. Current time stamp can be get like below example.

This will print an integer value like below.

Convert Timestamp To Date

We can convert given timestamp into date format. We will remember that date() function will get time stamp as second arguments. In this example we will give timestamp we get previously and print it more human readable format.

We will get following output

Mktime Function

Sometimes we need to generate timestamp with a given date and time. This means we do not want to get current time timestamp. In this situation we can use mktime() function. Syntax of mktime function is like below.

In this example we will provide first day of 2017 .

This code will generate following output.

PHP Tutorial Topics

  1. Introduction To PHP
  2. Installing PHP To Linux
  3. Installing PHP To Windows
  4. PHP Hello World Project with Eclipse
  5. PHP Syntax
  6. PHP Variables and Types
  7. PHP String Types
  8. PHP Numeric Type (Integer, Float, Double)
  9. PHP Boolean Type
  10. PHP Arrays
  11. PHP Operators
  12. PHP Ternary Operators
  13. PHP Constants
  14. PHP If..Elseif..Else Conditional Statements
  15. PHP Switch..Case Conditional Statements
  16. PHP For and Foreach Loop Statements
  17. PHP While and Do While Statements
  18. PHP Functions
  19. PHP Class and Objects
  20. PHP Date and Time Operations
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/php-date-time/
PINTEREST
PINTEREST
INSTAGRAM

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)