0Pricing
PHP Academy · Lesson

Unix Timestamps and date()

Get current time as a Unix timestamp and format it with date().

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 timestamp
  • date($format, $timestamp) — format a timestamp
  • mktime() — 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 date

All lessons in this course

  1. Unix Timestamps and date()
  2. The DateTime Class
  3. Date Arithmetic with DateInterval
  4. Timezones in PHP
← Back to PHP Academy