1

I have a date:

20-01-2013

I need it to be in a format for the database:

2013-01-20 20:39:39

So far I have this:

$originalDate = "20-01-2013";
$newDate = date("Y-m-d", strtotime($originalDate));

But how do I get the time added to the end? I just want it to be 00:00:00.

Thanks

beans
  • 1,765
  • 5
  • 25
  • 31
  • 1
    if you already have the `yyyy-mm-dd` portion and you know that your time is going to be zero, why not just append the string `' 00:00:00'` to the end of your existing string? – Spudley May 16 '13 at 13:36
  • @Spudley He/She does not have a `yyyy-mm-dd` date format. – str May 16 '13 at 13:39
  • Please note that all the answers to this question are wrong. You should be using the format `'Y-m-d H:i:s'` to get 24 hour time format. Fwiw, I would have used: `$yourDate = '20-01-2013'; $date = new DateTime($yourDate); echo $date->format('Y-m-d H:i:s');`, but as others have said, this question has been answered before. Good luck! – LeonardChallis May 16 '13 at 13:43

3 Answers3

1

Why don't you just do it like this?

$originalDate = "20-01-2013";
$newDate = date("Y-m-d", strtotime($originalDate)).' 00:00:00';
Siamak Motlagh
  • 5,028
  • 7
  • 41
  • 65
0

You are one step away from solution you just need to insert h:i:s in your date() function, so please try this:

$newDate = date("Y-m-d H:i:s", strtotime($originalDate));

or either append the string

$newDate = date("Y-m-d 00:00:00", strtotime($originalDate));
Fabio
  • 23,183
  • 12
  • 55
  • 64
0

$newDate = date("Y-m-d H:i:s", strtotime($originalDate));

read more about date

Bhuvnesh Gupta
  • 197
  • 2
  • 12