...

View Full Version : Date/Time Display Issue



tomyknoker
03-13-2007, 05:14 PM
Hi all,

I log the date and time people log into my site, I was able to display this using this code
<?php echo $qry['loginDateTime']; ?>, but I wanted to change the way the date and time were displayed so I added this
<?php echo date('d/m/Y H:i:s', strtotime($qry['loginDateTime'])); ?>, that fixed the issue but now something wierd is happening. Not many members have logged in since I added the loginDateTime column in my db. So with the first code it just displayed nothing for members that haven't logged and that is fine. But with the new code if they haven't logged in it populates the field anyway with today's date and a time of 00:00:00... Any ideas as to why this is happening?

Sylvester21
03-13-2007, 10:56 PM
Please bare with me, I'm a newbie to PHP. But it seems as if your code is only to display [echo] the date. How are you inserting it into the database? Which code are you using for that? And do you also use WHERE statements, to make sure only the column get's updated from the right user? Correct me if I'm wrong ofcourse, I'm just asking things as to what I see above :)

[edit]
I see it now. Why aren't you just inserting the right format into the database instead of changing it like this?

tomyknoker
03-13-2007, 11:00 PM
I thought MySQL only inserted it one way and then you had to retrieve it and edit it on retrieval?

Fumigator
03-13-2007, 11:07 PM
If you are using a database time, date or datetime data type, then yes you are right, the value is stored in a compressed format that you need not worry about-- you can then use date_format() (if you are using MySQL) to select and format the date however you want it to display.

You are trying to format the date in PHP, but it is much simpler to format it within your query.

Sylvester21
03-13-2007, 11:20 PM
And you can do that using the following code...

When inserting for example $date, use the following:


$date = date("D M Y");

You can change the D M Y to anything you want ofcourse, for the values go to php.net (http://www.php.net)

This should solve your problem, I hope. Glad I could help someone else out for a change :)

Fumigator
03-13-2007, 11:29 PM
Well... that's how you can format dates within PHP... but I was talking about formatting the date as you pull it out of the table using date_format() (http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html).

i.e.


$query = "SELECT date_format(date_field, '%m-%d-%Y') from my_table";

Sylvester21
03-13-2007, 11:36 PM
Well... that's how you can format dates within PHP... but I was talking about formatting the date as you pull it out of the table using date_format() (http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html).

i.e.


$query = "SELECT date_format(date_field, '%m-%d-%Y') from my_table";


I also don't know how that works, since I've been struggling with that myself..


I thought MySQL only inserted it one way and then you had to retrieve it and edit it on retrieval?

Was just reading your reply and saw the above, so I assumed you didn't know about that date() thing I posted.

ptmuldoon
03-14-2007, 02:54 AM
Well... that's how you can format dates within PHP... but I was talking about formatting the date as you pull it out of the table using date_format() (http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html).

i.e.


$query = "SELECT date_format(date_field, '%m-%d-%Y') from my_table";


But what if you only want to run one query, and then format the date into multiple formats? Ie., grab the date and time, then to break up the date and time into two or more separate variables and formats?

Fumigator
03-14-2007, 04:05 AM
As I stated in the other post, it's essentially like this



$query = "SELECT
date_format(date_field, '%m/%d : %l/%i') as format1,
date_format(date_field, '%D day of %M in the year of our lord, %Y at %r') as format2
FROM my_table";

CFMaBiSmAd
03-14-2007, 04:08 AM
You can put any number of date/time functions in a select query to get any part(s) you want. There is a slew of other date/time fucntions. Take a look a the manual - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

aedrin
03-14-2007, 04:53 PM
As I stated in the other post, it's essentially like this



$query = "SELECT
date_format(date_field, '%m/%d : %l/%i') as format1,
date_format(date_field, '%D day of %M in the year of our lord, %Y at %r') as format2
FROM my_table";


Isn't this redundant?

Doing it in PHP would then be a lot more efficient.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum