user875139
user875139

Reputation: 1621

Get particular date format in php?

How can I get a date to look like this format?

Year-month-day-hour-minute-second

2012-06-29-09-06-11

This is what I have so far.

$date = new DateTime;
$time = $date->format('y-m-d-h-m-s');

The above results gives this

12-06-29-09-06-11

I ran this at 3:07 pm eastern time.

As you can see its not quite what I want. Please help. Thanks.

Upvotes: 1

Views: 686

Answers (4)

Adi
Adi

Reputation: 99

You can use the PEAR library of DATE and read its docs to get the desired result.

Upvotes: 0

nageeb
nageeb

Reputation: 2042

Try this:

$date->format('Y-m-d-h-i-s');

Also don't forget to set your timezone using date_default_timezone_set() eg:

date_default_timezone_set('America/Los_Angeles');

Upvotes: 0

nickb
nickb

Reputation: 59699

You need to specify a timezone to the DateTime constructor, in addition to using a capital Y to get the four-digit year and i to get the minutes:

$date = new DateTime( "now", new TimeZone( "America/New_York"));
$time = $date->format('Y-m-d-h-i-s');

The list of supported timezones will allow you to select the one you need (I believe I chose the correct one for EST).

Your current string uses m for both months and minutes, which is not correct.

Upvotes: 2

Bob
Bob

Reputation: 128

$date = new DateTime;
$time = $date->format('Y-m-d-h-m-s');

y - 2 digits

Y - 4 digits

Upvotes: 0

Related Questions