Results 1 to 13 of 13

Thread: Date in PHP?

  1. #1
    Join Date
    Aug 2014
    Posts
    36

    * Date in PHP?

    I can't seem to store a date into the database. I'm trying to store a date when the user signs up and update the date every time they login and out.

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

    When I echo out just the date, it outputs the date but when I store it into the database, the date field I added shows 0000-00-00...

  2. #2
    Join Date
    Oct 2002
    Location
    /roof/ledge
    Posts
    28,088
    http://dev.mysql.com/doc/refman/5.1/en/datetime.html
    Likely relevant quote from that page:
    Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00').
    Your one stop shop for decentralization

  3. #3
    This should be if your database field is of DATE type.

    Code:
      $date = date('Y-m-d');
    You can try the following if your database field is of DATETIME type.

    Code:
      $date = date('Y-m-d H:i:s');
    You can use MySQL native NOW() function in queries as well. Look this up in Google.

  4. #4
    Join Date
    Jan 2010
    Location
    London
    Posts
    154
    echoing date in php is case sensitive. capital Y-M-D and y-m-d display different date formats. use lower case to store in the db. If you are going to compare dates then use data type date in db otherwise you can use varchar.

    Take a look here for all the date formats in php.

    http://php.net/manual/en/function.date.php

  5. #5
    Join Date
    Aug 2001
    Posts
    5,597
    date() might not be the right tool in the first place.

  6. #6
    Join Date
    Apr 2013
    Location
    California
    Posts
    150
    How come you are not making use of epoch time, which you can obtain from running time(); That is an integer, which is much better to store in the database, is sortable, and is easy to work with in a variety of different ways. Once you obtain it, you just throw it back into the date function

    $date = date("Y-M-D", $time_from_database);

    Far less messy to work with.
    Komputer King Web Hosting

    █ Shared hosting | Dedicated Servers | OpenVZ | KVM | Xen | cPanel | CDN
    █ 99.9% uptime | 24/7 Support | Live Chat | Varnish | PHP-FPM | Percona DB

  7. #7
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    3,785
    Quote Originally Posted by AWH - Kalpesh View Post
    You can use MySQL native NOW() function in queries as well. Look this up in Google.
    The best advice right here can avoid the use of PHP's date() entirely.

    Code:
    update user set lastlogin=NOW()
    Nice clean and easy

    Quote Originally Posted by komputerking View Post
    How come you are not making use of epoch time, which you can obtain from running time(); That is an integer, which is much better to store in the database, is sortable, and is easy to work with in a variety of different ways. Once you obtain it, you just throw it back into the date function

    $date = date("Y-M-D", $time_from_database);

    Far less messy to work with.

    Not as clean you would think. For example if you're storing it as an integer you can't as easily find all dates that are a Monday.
    Tony B. - Chief Executive Officer
    Hawk Host Inc. Proudly serving websites since 2004
    Quality Shared and Cloud Hosting
    PHP 5.2.x - PHP 8.1.X Support!

  8. #8
    this code :
    $date = date('Y-m-d');
    is very different to this :
    $date = date('Y-M-D');
    this first will add : 2014-08-31
    the second i Think will show : 2014-Aug-Sun

  9. #9
    Normally what I do is

    date("Y-m-d", strtotime(now));

  10. #10
    Join Date
    Apr 2013
    Location
    California
    Posts
    150
    you don't need to do...

    date("Y-m-d", strtotime(now));

    date("Y-m-d"); should work, as it uses the default time(). you're just adding in a strotime() function, which converts to time(), and should be slower
    Komputer King Web Hosting

    █ Shared hosting | Dedicated Servers | OpenVZ | KVM | Xen | cPanel | CDN
    █ 99.9% uptime | 24/7 Support | Live Chat | Varnish | PHP-FPM | Percona DB

  11. #11
    date("Y-m-d", strtotime(now));
    strtotime convert a string to date, but you have date() so you dont need strtotime

  12. #12
    The data type "Date" in database accepts the following format "YYYY-MM-DD hh:mms" (at least in MySQL), though you may not need to use the date function in PHP as there is already a built-in method in getting the current date/time sql query, it's something like "field_name = NOW()".
    EmailHosting.com
    http://www.emailhosting.com

  13. #13
    Join Date
    Oct 2002
    Location
    Chicago, IL
    Posts
    111
    Like others have mentioned, you're most likely better off utilizing MySQL's date functions such as NOW(), CURRENT_TIMESTAMP(), FROM_UNIXTIME(), etc rather than dealing with it in PHP. This is a good rule of thumb in general.

    Also, perhaps your date column should use MySQL's built-in column timestamp updates? Then your inserts and updates could populate the date automatically. (Search for ON UPDATE CURRENT_TIMESTAMP)
    Rescuing Websites from Bad Hosts since 2001
    Don't let your website get abducted!

    Krellen.com - Web hosting you can count on.

Similar Threads

  1. [PHP] If this date! then New, else nothing.
    By e-zone in forum Programming Discussion
    Replies: 9
    Last Post: 07-15-2008, 10:26 AM
  2. help with php DATE problem
    By zoya23 in forum Programming Discussion
    Replies: 2
    Last Post: 01-25-2005, 05:24 AM
  3. why system date and php date are different
    By e-view in forum Hosting Security and Technology
    Replies: 1
    Last Post: 10-08-2004, 03:28 AM
  4. PHP date and server date are different!
    By vovanus in forum Programming Discussion
    Replies: 2
    Last Post: 09-22-2004, 12:35 PM
  5. PHP Date...
    By CreativeLogic in forum Programming Discussion
    Replies: 8
    Last Post: 05-19-2004, 05:06 PM

Posting Permissions

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