Unix Timestamps and date()
Get current time as a Unix timestamp and format it with date().
Unix Timestamps and date() is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Time in PHP
PHP represents time as a Unix timestamp — the number of seconds since January 1, 1970, 00:00:00 UTC (the Unix epoch). Key functions:
time()— current Unix timestampdate($format, $timestamp)— format a timestampmktime()— create a timestamp from date parts
time()
Get the current Unix timestamp:
<?php
$now = time();
echo $now; // e.g. 1716800000
echo gettype($now); // integer
// 1 day in seconds
$tomorrow = time() + 86400;
$yesterday = time() - 86400;
echo date('Y-m-d', $tomorrow); // tomorrow's datedate() Format Characters
Common format characters for date():
Y— 4-digit year: 2024m— month with leading zero: 01–12d— day with leading zero: 01–31H— hour 24h with leading zeroi— minutes with leading zeros— secondsD— short day name: MonN— ISO day of week: 1=Mon, 7=Sun
Formatting Dates
Use date() to format timestamps:
<?php
$ts = time();
echo date('Y-m-d', $ts); // 2024-05-27
echo date('d/m/Y', $ts); // 27/05/2024
echo date('D, d M Y H:i:s', $ts); // Mon, 27 May 2024 14:30:00
echo date('c', $ts); // ISO 8601: 2024-05-27T14:30:00+00:00
echo date('U', $ts); // Unix timestamp (same as time())mktime()
Create a timestamp from specific date/time components:
<?php
// mktime(hour, min, sec, month, day, year)
$ts = mktime(0, 0, 0, 12, 25, 2024); // Christmas 2024 midnight
echo date('Y-m-d', $ts); // 2024-12-25
// Last day of month trick:
$lastDay = mktime(0, 0, 0, 3, 0, 2024); // day 0 = last day of Feb
echo date('Y-m-d', $lastDay); // 2024-02-29 (leap year)strtotime()
strtotime() converts a human-readable date string to a timestamp:
<?php
echo strtotime('2024-12-25'); // timestamp
echo strtotime('next Monday'); // next Monday's timestamp
echo strtotime('+1 week'); // 7 days from now
echo strtotime('last day of this month'); // end of month
echo strtotime('2024-01-01 +3 months'); // April 1, 2024Date Arithmetic with strtotime
Calculate relative dates using strtotime's relative expressions:
<?php
$subscriptionStart = strtotime('2024-01-15');
$oneYearLater = strtotime('+1 year', $subscriptionStart);
$trialEnd = strtotime('+14 days');
echo date('Y-m-d', $oneYearLater); // 2025-01-15
echo date('Y-m-d', $trialEnd); // 14 days from today
// Days until event:
$event = strtotime('2024-12-31');
$daysLeft = (int) (($event - time()) / 86400);
echo "Days until NYE: $daysLeft";Timestamps and Databases
Store dates in the database as ISO strings or Unix timestamps:
<?php
// Store as ISO string (recommended for readability):
$created_at = date('Y-m-d H:i:s'); // '2024-05-27 14:30:00'
// Store as Unix timestamp (fast integer comparison):
$created_ts = time(); // 1716800000
// Convert back:
$ts = strtotime($created_at);
$fmt = date('Y-m-d H:i:s', $created_ts);date_create and date_format
OO alternatives to date() and strtotime():
<?php
$dt = date_create('2024-05-27');
echo date_format($dt, 'D, d M Y'); // Mon, 27 May 2024
date_modify($dt, '+30 days');
echo date_format($dt, 'Y-m-d'); // 2024-06-26
// Equivalent OO:
$dt2 = new DateTime('2024-05-27');
$dt2->modify('+30 days');
echo $dt2->format('Y-m-d');checkdate()
Validate whether a date is real with checkdate():
<?php
var_dump(checkdate(2, 29, 2024)); // true (leap year)
var_dump(checkdate(2, 29, 2023)); // false (not leap year)
var_dump(checkdate(13, 1, 2024)); // false (no month 13)
var_dump(checkdate(1, 31, 2024)); // trueMicrotime for Benchmarking
Use microtime(true) to measure execution time with sub-second precision:
<?php
$start = microtime(true);
// Simulate work
for ($i = 0; $i < 100000; $i++) {}
$end = microtime(true);
$duration = ($end - $start) * 1000; // milliseconds
echo sprintf('Took %.2f ms', $duration);Quick Check
What does PHP's time() function return?
Recap: Unix Timestamps and date()
Key points:
time()— current Unix timestampdate($format, $ts)— format any timestampmktime()— build timestamp from date partsstrtotime()— parse human-readable dates- Use ISO 8601 format for database storage
microtime(true)for high-resolution timing
Frequently asked questions
Is the “Unix Timestamps and date()” lesson free?
Yes — the full text of “Unix Timestamps and date()” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Unix Timestamps and date()”?
Get current time as a Unix timestamp and format it with date(). You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Unix Timestamps and date()” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Unix Timestamps and date()
- The DateTime Class
- Date Arithmetic with DateInterval
- Timezones in PHP