Skip to content
← Back to Snippets
Code

Convert UNIX Timestamp to Date

Converts a UNIX timestamp to formatted date strings with an explicit timezone.

Purpose

Converts a UNIX timestamp to formatted date strings with an explicit timezone.

Snippet details

ContextDate TimeLevelProductionCopy-and-paste statusMarked safe after review.

Categories

  • Forms and Validation

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Convert UNIX Timestamp to Date.
 *
 * Purpose:
 * Converts a UNIX timestamp into display and database date formats.
 *
 * @param int $timestamp UNIX timestamp.
 * @param string $timezone_name PHP timezone name.
 * @return array Formatted date values.
 */
function ogSnippetConvertUnixTimestampToDate(int $timestamp, string $timezone_name): array {
	$timezone_name = trim($timezone_name);

	if (in_array($timezone_name, timezone_identifiers_list(), true) === false) {
		$timezone_name = 'UTC';
	}

	$timezone = new DateTimeZone($timezone_name);
	$date = new DateTimeImmutable('@'.$timestamp);
	$date = $date->setTimezone($timezone);

	return array(
		'timestamp' => $timestamp,
		'timezone' => $timezone_name,
		'display' => $date->format('F j, Y g:i A T'),
		'database' => $date->format('Y-m-d H:i:s')
	);
}

$date_report = ogSnippetConvertUnixTimestampToDate(1700000000, 'America/New_York');

echo 'Converted date: '.$date_report['display'];