PHP:
Parsing a date from a string
How to:
PHP’s built-in DateTime
class provides a powerful set of functions for parsing and working with dates. You can create a DateTime
instance from a date string using the constructor, and then format it as needed. Here’s how:
$dateString = "2023-04-25 15:30:00";
$dateObject = new DateTime($dateString);
echo $dateObject->format('Y-m-d H:i:s');
// Output: 2023-04-25 15:30:00
To handle strings that follow non-standard formats, you can use the createFromFormat
method, which allows you to specify the exact format of the input date:
$dateString = "25-04-2023 3:30 PM";
$dateObject = DateTime::createFromFormat('d-m-Y g:i A', $dateString);
echo $dateObject->format('Y-m-d H:i:s');
// Output: 2023-04-25 15:30:00
For more complex parsing that might not be directly supported by DateTime
, PHP offers the strtotime
function, which attempts to parse any English textual datetime description into a Unix timestamp:
$timestamp = strtotime("next Thursday");
echo date('Y-m-d', $timestamp);
// Output will vary depending on the current date, e.g., "2023-05-04"
Using third-party libraries:
While PHP’s built-in functions cover a wide range of use cases, you might sometimes need more sophisticated parsing capabilities. The Carbon library, an extension of PHP’s DateTime class, provides a rich set of features for date/time manipulation:
require 'vendor/autoload.php';
use Carbon\Carbon;
$dateString = "Tomorrow";
$date = Carbon::parse($dateString);
echo $date->toDateTimeString();
// Output will vary, e.g., "2023-04-26 00:00:00"
Carbon’s parse
method can smartly handle a multitude of date and time formats, making it an invaluable tool for applications that require flexible date parsing functionality.