Encodera

Thanks for reading!

Donate to the site — we work hard for you.

How to format a date

Short answer

date() takes a format and a timestamp. strtotime() turns a date string into a timestamp:

<?php
 
echo date('d.m.Y', strtotime('2025-03-15'));

Output:

15.03.2025

The DateTime class

A DateTime object is more convenient when you need to do more with the date: compare it, add days, change the time zone.

<?php
 
$date = new DateTime('2025-03-15 14:30');
 
echo $date->format('d.m.Y H:i');

Output:

15.03.2025 14:30

The main format characters

CharacterMeaningExample
dDay with a leading zero05
mMonth with a leading zero03
YFour-digit year2025
HHours, 24-hour clock14
iMinutes30
sSeconds07

The current date

Without a second argument date() uses the current time:

<?php
 
echo date('Y-m-d');

Thanks for reading!

Donate to the site — we work hard for you.