Welcome Guest, Not a member yet? Register   Sign In
Converting DateTime Type to MM/DD/YYYY
#1

[eluser]Kesey[/eluser]
I've got a field in my database called "post_date" and it's of the type DateTime. When reading this field back and displaying it on my site, I want it to appear without the timestamp.

I'm trying this:
Code:
<h3>&lt;?= date('M d', $row->post_date); ?&gt;</h3>

within this loop:

Code:
&lt;?php foreach($query->result() as $row): ?&gt;
        
        <h3>&lt;?=$row->post_title?&gt;</h3>
        <h3>&lt;?= date('M d', $row->post_date); ?&gt;</h3>
        <p>&lt;?=$row->post_content?&gt;</p>
        <hr>        
    &lt;?php endforeach; ?&gt;

And I'm getting the error:
Message: A non well formed numeric value encountered

But it's also printing Dec 31 (I'm expecting May 3).

Any help would be appreciated. Thanks.
#2

[eluser]John_Betong[/eluser]
&nbsp;
Hi Kesey,
&nbsp;
Your PHP function date() is expecting the second parameter to be an integer. This second parameter expexted is the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
&nbsp;
Your second parameter value is a MySQL string data type in the form of 'yyyy-mm-dd hh:mmConfuseds'.
&nbsp;
I think the reason Dec 31 is being printed is that the string is converted to a numeric zero value.
&nbsp;
Take a look at CodeIgniter's Date Helper and see if you can find a solution to your problem.
&nbsp;
Cheers,
&nbsp;
John
&nbsp;
&nbsp;
&nbsp;
#3

[eluser]Michael Wales[/eluser]
Code:
$this->load->helper('date');
$date_to_echo = date('M d', mysql_to_unix($row->post_date));
#4

[eluser]xwero[/eluser]
Without loading helpers you could do
Code:
&lt;?= date('M d', mktime(0,0,0,substr($row->post_date,5,2),substr($row->post_date,8,2),substr($row->post_date,0,4)); ?&gt;
the mysql_to_unix does something similar.
#5

[eluser]Mark van der Walle[/eluser]
Code:
&lt;?= date('M d', strtotime($row->post_date)); ?&gt;
Works fine by the way. Just the build in functions needed. Also I would use strftime instead of date so you have support for localization.
#6

[eluser]xwero[/eluser]
I have used the strtotime function and it doesn't work all the time so i rather use mktime. I think it has to do with the English time format.
#7

[eluser]Mark van der Walle[/eluser]
True strtotime does not work properly for pre 1970 and post 2038 dates Wink
#8

[eluser]xwero[/eluser]
It is more in the style of Kesey error that it displays a wrong month.
#9

[eluser]Skulls[/eluser]
if strtotime does not work for you, why don't you extract the date in the format you like directly from mysql using the date and time functions
#10

[eluser]xwero[/eluser]
That is an option that links the model method to the view which is not good if you want to use it in other views that can display the date different. But i think most databases have a function that can transform the database datatime format to a unix timestamp and the need of other functions isn't necessary, that would be the best solution, or not?




Theme © iAndrew 2016 - Forum software by © MyBB