0

I save my dates as datetime in my db (e.g. 2013-09-23 15:00:00).

When I create an object I call this date by

$event->start

When I just echo it, I get the right value but it is a string and no longer a datetime. That is why this won't work:

date_format('d-m-Y', $event->start)

To use date() I need a timestamp, which I don't have. What is a possible solution?

I tried this:

date_format(strtotime($event->startdate), 'd-m-Y')

But still getting 'Date_format() expects parameter 1 to be DateTime'

SOLUTION

$dt = new DateTime($event->start);
echo $dt->format('d-m-Y');
bflydesign
  • 483
  • 8
  • 22

3 Answers3

2

Have a look at strtotime(), which will give you a timestamp from a vaguely human readable date string. You can then use that in your date_format call, e.g.

$ts = strtotime($event->start);

if($ts !== FALSE)   // !== -1 in versions of PHP prior to 5.1.0
    $date = date_format("d-m-Y", $ts);
slugonamission
  • 9,562
  • 1
  • 34
  • 41
0

use strtotime to get datetime from a string

just try this

echo date('d-m-Y', strtotime($event->start))

Md. Sahadat Hossain
  • 3,210
  • 4
  • 32
  • 55
0

Use strtotime() for this.

Try this code.

$date = $event->start;
$my_date = date('d-m-Y', strtotime($date));
echo $my_date;
Dhaval Bharadva
  • 3,053
  • 2
  • 24
  • 35