PHP Timestamp into DateTime
Parsing RSS feed dates often trips up developers because of how DateTime and strtotime interact with timezone strings. You might have a string like Mon, 12 Dec 2011 21:17:52 +0000, and your instinct is to extract the numeric timestamp using substr or strtotime, then inject it into a DateTime constructor. However, if you loop through multiple entries and they all resolve to the same date, something fundamental about how PHP handles timezones is being overlooked.
The specific failure in your approach lies in this line:$dtm = new DateTime(strtime($time));
When you pass a Unix timestamp (seconds since the epoch) to the DateTime constructor, that constructor defaults to the system's local timezone. If your server is running in UTC or a different region than where the date string was originally generated, the hour offset changes silently. Even if you extract the raw seconds correctly, the object itself isn't anchored to the specific timezone indicated in the source string (+0000).
To fix this without relying on strtotime for the initial conversionâwhich can sometimes be ambiguous with partial stringsâyou should leverage the DateTimeZone directly within the constructor. This ensures the moment in time is interpreted exactly as intended by the source data before any formatting occurs.
Here is a robust pattern to handle your specific input:
$rawDate = 'Mon, 12 Dec 2011 21:17:52 +0000';
// Create the DateTime object directly from the string with its own timezone
$dtm = new DateTime($rawDate);
// Now apply your specific server timezone for formatting
$dtm->setTimezone(new DateTimeZone(ADMIN_TIMEZONE));
// Format the output as requested
$date = $dtm->format('D, M dS');
$time = $dtm->format('g:i a');
By passing $rawDate directly to new DateTime(), PHP parses the full ISO-like string and respects the +0000 offset embedded in it. The object now holds the exact moment in absolute time, regardless of server settings. When you subsequently call setTimezone(new DateTimeZone(ADMIN_TIMEZONE)), you are shifting the view of that moment to match your application's context, not altering the underlying data.
If you strictly need to extract just the timestamp seconds firstâfor example, to perform arithmetic or store a scalar valueâyou can use strtotime on the full string rather than a substring. Passing a truncated string like 12 Dec 2011 to strtotime loses the timezone information entirely, causing PHP to fall back to local time assumptions. Always ensure the string passed to strtotime includes the offset (e.g., +0000) if accuracy is paramount.
Another consideration involves the ADMIN_TIMEZONE constant. Ensure it is defined as a valid class name or string representing a timezone, such as 'America/New_York'. Hardcoding offsets like -5:00 is generally discouraged because daylight saving time shifts will break your logic later in the year. Using the DateTimeZone class guarantees that seasonal changes are handled automatically by the OS.
If you are working within a Laravel application, you can simplify this further using the Carbon package, which handles these timezone conversions elegantly:
use Carbon\Carbon;
$dtm = Carbon::parse('Mon, 12 Dec 2011 21:17:52 +0000');
$date = $dtm->setTimezone('ADMIN_TIMEZONE')->format('D, M dS');
$time = $dtm->setTimezone('ADMIN_TIMEZONE')->format('g:i a');
This approach abstracts away the manual timezone shifting and reduces the risk of accidentally modifying the parsed timestamp.
