Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-13-2007, 04:14 PM   PM User | #1
tomyknoker
Regular Coder

 
Join Date: Mar 2006
Posts: 459
Thanks: 3
Thanked 0 Times in 0 Posts
tomyknoker is an unknown quantity at this point
Date/Time Display Issue

Hi all,

I log the date and time people log into my site, I was able to display this using this code
Code:
<?php echo $qry['loginDateTime']; ?>
, but I wanted to change the way the date and time were displayed so I added this
Code:
<?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?
tomyknoker is offline   Reply With Quote
Old 03-13-2007, 09:56 PM   PM User | #2
Sylvester21
New Coder

 
Join Date: Mar 2007
Location: The Netherlands
Posts: 91
Thanks: 21
Thanked 0 Times in 0 Posts
Sylvester21 is an unknown quantity at this point
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?
__________________
Thank you for your time to read my post

Last edited by Sylvester21; 03-13-2007 at 09:59 PM.. Reason: Read it wrong
Sylvester21 is offline   Reply With Quote
Old 03-13-2007, 10:00 PM   PM User | #3
tomyknoker
Regular Coder

 
Join Date: Mar 2006
Posts: 459
Thanks: 3
Thanked 0 Times in 0 Posts
tomyknoker is an unknown quantity at this point
I thought MySQL only inserted it one way and then you had to retrieve it and edit it on retrieval?
tomyknoker is offline   Reply With Quote
Old 03-13-2007, 10:07 PM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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.
__________________
Fumigator is offline   Reply With Quote
Old 03-13-2007, 10:20 PM   PM User | #5
Sylvester21
New Coder

 
Join Date: Mar 2007
Location: The Netherlands
Posts: 91
Thanks: 21
Thanked 0 Times in 0 Posts
Sylvester21 is an unknown quantity at this point
And you can do that using the following code...

When inserting for example $date, use the following:

Code:
$date = date("D M Y");
You can change the D M Y to anything you want ofcourse, for the values go to php.net

This should solve your problem, I hope. Glad I could help someone else out for a change
__________________
Thank you for your time to read my post

Last edited by Sylvester21; 03-13-2007 at 10:21 PM.. Reason: Errors :P
Sylvester21 is offline   Reply With Quote
Old 03-13-2007, 10:29 PM   PM User | #6
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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().

i.e.
PHP Code:
$query "SELECT date_format(date_field, '%m-%d-%Y') from my_table"
__________________
Fumigator is offline   Reply With Quote
Old 03-13-2007, 10:36 PM   PM User | #7
Sylvester21
New Coder

 
Join Date: Mar 2007
Location: The Netherlands
Posts: 91
Thanks: 21
Thanked 0 Times in 0 Posts
Sylvester21 is an unknown quantity at this point
Quote:
Originally Posted by Fumigator View Post
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().

i.e.
PHP Code:
$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..

Quote:
Originally Posted by tomyknoker View Post
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.
__________________
Thank you for your time to read my post
Sylvester21 is offline   Reply With Quote
Old 03-14-2007, 01:54 AM   PM User | #8
ptmuldoon
Regular Coder

 
Join Date: Feb 2005
Posts: 660
Thanks: 5
Thanked 14 Times in 14 Posts
ptmuldoon is on a distinguished road
Quote:
Originally Posted by Fumigator View Post
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().

i.e.
PHP Code:
$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?
ptmuldoon is offline   Reply With Quote
Old 03-14-2007, 03:05 AM   PM User | #9
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
As I stated in the other post, it's essentially like this

PHP Code:
$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"

__________________
Fumigator is offline   Reply With Quote
Old 03-14-2007, 03:08 AM   PM User | #10
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,714
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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/...functions.html
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 03-14-2007, 03:53 PM   PM User | #11
aedrin
Senior Coder

 
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
aedrin will become famous soon enough
Quote:
Originally Posted by Fumigator View Post
As I stated in the other post, it's essentially like this

PHP Code:
$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.
aedrin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:08 AM.


Advertisement
Log in to turn off these ads.