PHP – String to DateTime and vice versa

// Assume you have a date string which is in d-m-Y format.
$date_string = '18-04-2013';

// Create the DateTime object
$date = date_create_from_format('d-m-Y', $date_string);

// Print the date in Y-m-d format.
print date('Y-m-d', $date->getTimestamp());

 

Done =)

Reference:

Advertisement

One thought on “PHP – String to DateTime and vice versa”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.