Code Bytes
Convert a list of UK dates to Unix timestamps in PHP
Validate day/month/year dates, choose a timezone explicitly and convert a whole list without silently accepting impossible dates.
A spreadsheet full of dates is straightforward to import until 03/07/2026 gets interpreted as March instead of July. Decide the input format and timezone before converting anything.
Validate first, convert second
Save this as convert-dates.php and run php convert-dates.php in a terminal. It accepts one- or two-digit days and months, and a four-digit year. The sample deliberately uses midnight UTC, not the server's local timezone.
<?php
declare(strict_types=1);
function ukDateTimestamp(string $input): int
{
if (!preg_match('/\A([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})\z/', $input, $parts)
|| !checkdate((int) $parts[2], (int) $parts[1], (int) $parts[3])) {
throw new InvalidArgumentException('Expected a valid day/month/year date.');
}
$date = DateTimeImmutable::createFromFormat(
'!j/n/Y', $input, new DateTimeZone('UTC')
);
if ($date === false) {
throw new InvalidArgumentException('The date could not be parsed.');
}
return $date->getTimestamp();
}
$dates = ['18/6/2026', '25/09/2026', '03/07/2026'];
$timestamps = array_map('ukDateTimestamp', $dates);
foreach ($timestamps as $index => $timestamp) {
printf("%s => %d\n", $dates[$index], $timestamp);
}
The entire array is validated before the output loop, so a bad row does not produce a half-converted list. For a real import, report the row number alongside a validation error and fix the source rather than substituting today's date.
A date is not always a moment
For birthdays, renewal dates and other calendar-only values, storing YYYY-MM-DD may be more appropriate than a timestamp. If an event begins at midnight in Britain, use Europe/London and accept that summer and winter have different UTC offsets. PHP timestamps are seconds; JavaScript's Date constructor expects milliseconds.
PHP's ! format resets unspecified time fields; checkdate() rejects dates such as 31 February before conversion. See date parsing and calendar validation.
Original version5 May 2016
Kept here for reference and earlier links. The updated guide above is the recommended starting point; older code may depend on retired services or different software versions.
Working with dates is a hugely common feature of systems. The problem as humans is we don't have a universally agreed upon format. The UK will use dd/mm/yyyy, America uses mm/dd/yyyy and even combinations of these.
Luckily with computer systems, we have a generally agreed upon timeformat called a UNIX timestamp, sometimes referred to as an Epoch timestamp or POSIX time.
This timestamp is the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.
For this reason, you may sometimes have a list of dates you want to convert to the EPOCH timestamp. This is fairly easy with online tools if you only have 1 timestamp to do. However when you have hundreds to convert (imagine migrating from one system to another and wanting to change the way you store dates in your database).
The below PHP code/snippet will help you easily generate a list of timestamps for the dates you feed into it.
<?php
// Create the function to accept the date format you're expecting. In this case it's dd/mm/yyyy, you can just swap the j/n/y around if you want to accept different combinations.
function dateToTimestamp($date) {
return DateTime::createFromFormat('j/n/Y', $date)->getTimestamp();
}
// Example dates, list your own here in the array:
$dates=[
'18/6/2015',
'25/9/2015',
'19/6/2015',
'3/7/2015',
'25/6/2015',
'10/7/2015'
];
// This foreach will loop over all the dates you provided and print out the EPOCH timestamp version.
foreach ($dates as $date) {
echo dateToTimestamp($date);
echo '<br>';
}
?>Keep exploring