Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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?

  • #2
    New Coder
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    91
    Thanks
    21
    Thanked 0 Times in 0 Posts
    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?
    Last edited by Sylvester21; 03-13-2007 at 09:59 PM. Reason: Read it wrong
    Thank you for your time to read my post

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I thought MySQL only inserted it one way and then you had to retrieve it and edit it on retrieval?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #5
    New Coder
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    91
    Thanks
    21
    Thanked 0 Times in 0 Posts
    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
    Last edited by Sylvester21; 03-13-2007 at 10:21 PM. Reason: Errors :P
    Thank you for your time to read my post

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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"

  • #7
    New Coder
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    91
    Thanks
    21
    Thanked 0 Times in 0 Posts
    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

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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?

  • #9
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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"


  • #10
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    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.

  • #11
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •