PHP Date & Time
Working with dates and times is essential for most applications. PHP's DateTime class provides powerful tools for date manipulation and formatting!
Output
Click Run to execute your code
Current Date & Time
<?php
echo date('Y-m-d H:i:s'); // 2025-12-15 12:30:00
echo date('F j, Y'); // December 15, 2025
?>
DateTime Object
<?php
$now = new DateTime();
echo $now->format('Y-m-d H:i:s');
// Modify date
$now->modify('+7 days');
echo $now->format('Y-m-d');
?>
Date Difference
<?php
$date1 = new DateTime('2025-01-01');
$date2 = new DateTime('2025-12-31');
$diff = $date1->diff($date2);
echo $diff->days . " days";
?>
Summary
- date(): Format current date
- DateTime: Object-oriented approach
- modify(): Add/subtract time
- diff(): Calculate difference
What's Next?
Next, learn about APIs & JSON - integrating with external services!
Enjoying these tutorials?