PHP Date and Time

PHP Date and Time Tutorial

PHP provides built-in functions to work with dates and times. You can display, format, manipulate, and calculate date and time values easily using PHP.


1️⃣ The date() Function

The date() function formats a local date and time according to a specified format.


 

Common Format Characters

Character Description Example
Y 4-digit year 2025
y 2-digit year 25
m Month (01-12) 11
d Day (01-31) 28
H Hour (00-23) 10
h Hour (01-12) 10
i Minutes (00-59) 15
s Seconds (00-59) 30
a am/pm am
l Full weekday name Thursday
D Short weekday name Thu
F Full month name November
M Short month name Nov

2️⃣ Displaying the Current Date and Time


 

Output Example:


 


3️⃣ Timestamps

A timestamp is the number of seconds since January 1, 1970, 00:00:00 UTC.


 

Convert timestamp to readable date:


 


4️⃣ Date Manipulation with strtotime()

strtotime() converts a string into a timestamp, which you can format using date().


 


5️⃣ Using mktime()

mktime() creates a timestamp for a specific date and time.


 


6️⃣ Date Difference


 


7️⃣ PHP DateTime Class (Object-Oriented)


 


🏁 Summary

  • Use date() to format current date and time.

  • time() gives the current timestamp.

  • strtotime() and mktime() allow date calculations.

  • DateTime class is modern and flexible for date/time manipulation.

  • Always set timezone with date_default_timezone_set().

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *