Welcome Guest, Not a member yet? Register   Sign In
Date Formatting
#1

[eluser]Chad Crowell[/eluser]
I am capturing a start_date and end_date for records in my app as date fields in mysql (not datetime). When I grab them with CI and echo them, the format is 2008-08-19, which is fine but I do want to rearrange it.

I tried this, but it gave me an error: A non well formed numeric value encountered

date("m/d/y",$client->date_added)

Is there a date formatting function in CI that I am missing?

Is there a better way to store dates in mysql if I don't need the time? If so, can I have some code examples of converting in and out of mysql?
#2

[eluser]xwero[/eluser]
Code:
date("m/d/y",strtotime($client->date_added));
Or you can do the date formating in the sql statement using the sql functions.
#3

[eluser]fesweb[/eluser]
To explain xwero's example, the date() function expects a unix/php formatted date to be passed. You will always need to convert a MySQL date to unix/php with strtotime($mysql_date) or mysql_to_unix($mysql_date) if you load the date helper...
#4

[eluser]Mirage[/eluser]
In order to reformat with the PHP date function you must convert your date to a unix timestamp. As xwero pointed out you can do that in PHP easily with the strtotime() function. Another option would be to do the conversion on the db end by adjusting your select statement. There's no standard for this across db's however and so not as portable.

HTH,
-m
#5

[eluser]Chad Crowell[/eluser]
Thanks everyone - this all helped a lot. Its just something I always find myself searching for when I have to deal with it.




Theme © iAndrew 2016 - Forum software by © MyBB