Convert date to day name e.g. Mon, Tue, Wed

Convert date to day name e.g. Mon, Tue, Wed

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why date('D') Fails on Packed Strings and How to Fix It

You are encountering a classic PHP string parsing issue where the format of your input data doesn't match what standard date functions expect by default. The variable $longdate holds the value 201308131830. When you pass this directly into strtotime(), it attempts to interpret the string as a human-readable date or a timestamp relative to the current time.

In this specific case, PHP's internal logic sees 20130813 and treats it as August 13th of 2013. However, because the input lacks delimiters (like dashes or slashes) and includes the time component at the end without a clear separator, strtotime() often defaults to the current year for the day name calculation if it cannot definitively resolve the full timestamp, or it interprets the sequence differently depending on the system's locale settings. The result is frequently that you get "Tue" (the correct day for Aug 13) but potentially inconsistent behavior in edge cases, or worse, completely wrong days if the parser gets confused by the lack of separators and assumes the input is relative to today's date in a weird way.

To solve this reliably, you must explicitly tell PHP how to parse the string before converting it to a timestamp. The most robust approach is using DateTime::createFromFormat(). This method requires you to define an input format mask that matches your data exactly.

Your string follows the pattern: YYYYMMDDHHmm.

  • 2013 = Year
  • 08 = Month
  • 13 = Day
  • 18 = Hour
  • 30 = Minute

You can create a format mask by concatenating these codes: 'YmdHis'. Note that we omit seconds (s) since they are not present in your string. Here is the corrected implementation:

$longdate = '201308131830';

// Define the exact format of the input string
$format = 'YmdHis';

// Parse the string using the explicit format
$dateObj = DateTime::createFromFormat($format, $longdate);

if ($dateObj) {
    $dayname = $dateObj->format('D'); // Outputs: Tue (or Mon depending on the actual year/day)
} else {
    // Fallback if parsing fails due to malformed input
    $dayname = date('D', strtotime($longdate)); 
}

Why Explicit Formats Matter

Relying on strtotime() for non-standard or packed strings is a fragile pattern. The function is designed to be flexible, often accepting "Aug 13" or "August 13th", but it becomes unpredictable when faced with raw numeric sequences without separators. By using DateTime::createFromFormat(), you remove all ambiguity. You are essentially saying, "I know exactly what these numbers represent," which prevents PHP from making assumptions based on the current system time or locale.

Formatting Variations

Once you have parsed the date correctly into a DateTime object, you can customize the output format string 'D' to fit your specific needs:

  • 'D' returns a 3-letter abbreviation (e.g., Tue, Mon).
  • 'l' returns the full name (e.g., Tuesday, Monday).

If you need both, you can chain multiple format characters: $dateObj->format('D l') would output something like Tue Tuesday.

Trade-offs and Limitations

The main trade-off here is the initial string length. You must ensure your input string matches the format mask perfectly. If your data source ever changes to include seconds (e.g., 20130813183045), your current mask 'YmdHis' will fail, resulting in a false positive where createFromFormat returns false. Always validate that the parsing was successful before accessing properties of the object.

If you are working within a Laravel ecosystem and want to handle this more elegantly without writing raw PHP logic every time, consider using the Carbon library or Laravel's built-in helpers which wrap these concepts. For instance, if you have a database field defined as DATETIME with that specific format stored as a string, you might need to cast it properly in your Eloquent model accessors. However, for a raw variable like $longdate, the explicit DateTime approach remains the industry standard for correctness.

If you are looking for more robust date handling patterns or need to integrate this into a larger Laravel application, resources like https://laravelcompany.com often provide deep dives into working with dates and times within that framework.

Our network