Dates in php part 1

Let’s say you’re querying a MySql Database and it has a datetime field called ‘datecreated’ – you will actually end up with an error if you try to apply date_format to the output.

e.g.  Let’s say we have a record with the value – 2011-06-06 15:32:12 – in the database column

if we do this:

<?php echo date_format($row_quote[‘datecreated’], “d-M-Y”) ; ?>

We will get an error something like:

Warning: date_format() expects parameter 1 to be DateTime, string given

Instead we need to use the following:

<?php echo date_format(new DateTime($row_quote[‘datecreated’]), “d-M-Y”) ; ?>

There’s some pretty useful stuff here on formatting dates.

Advertisements
Posted in Php

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: