lukehillonline
lukehillonline

Reputation: 2647

Using PHP to convert from one date format to another

I'm looking to convert a date from one format to another.

This is my current date format:

2011-08-15 15:35:58

and I need to convert it into:

Mon, 15/08/2011 15:35

I can quite easily create this by cutting the last 3 characters from the time and then using str_replace to change '-' into '/' and so on, but I was wondering if there is there is a way to do this automatically using the date() function or something similar. I am also not sure how to generate the day of the week e.g. 'Mon', 'Tues' etc.

Hope someone can help. Thanks.

Upvotes: 1

Views: 5445

Answers (3)

neworld
neworld

Reputation: 7793

This code work fine:

<?

$time = strtotime("2011-08-15 15:35:58");
echo date("D, d/m/y H:m", $time);

But be carefull, strtotime depends on timezone settings.

Upvotes: 0

Dunhamzzz
Dunhamzzz

Reputation: 14808

This can be done easily using a combination of date() and strtotime():

$newDate = date('D, d/m/Y H:i', strtotime($oldDate));

Upvotes: 4

Robik
Robik

Reputation: 6127

Use strtotime and date functions:

$d = strtotime("2011-08-15 15:35:58");

var_dump(date( 'D, d/n/Y H:i',$d));

Upvotes: 1

Related Questions