Results 1 to 13 of 13
Thread: Date in PHP?
-
08-18-2014, 05:48 PM #1Junior Guru Wannabe
- 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...
-
08-18-2014, 06:13 PM #2
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
-
08-18-2014, 08:47 PM #3Junior Guru Wannabe
- Join Date
- Jan 2008
- Posts
- 57
This should be if your database field is of DATE type.
Code:$date = date('Y-m-d');
Code:$date = date('Y-m-d H:i:s');
-
08-18-2014, 10:29 PM #4WHT Addict
- 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
-
08-19-2014, 04:05 AM #5Web Hosting Master
- Join Date
- Aug 2001
- Posts
- 5,597
date() might not be the right tool in the first place.
-
08-30-2014, 03:31 AM #6WHT Addict
- 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
-
08-30-2014, 02:06 PM #7Web Hosting Master
- Join Date
- Aug 2004
- Location
- Canada
- Posts
- 3,785
The best advice right here can avoid the use of PHP's date() entirely.
Code:update user set lastlogin=NOW()
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!
-
08-31-2014, 05:32 PM #8Aspiring Evangelist
- Join Date
- Apr 2014
- Posts
- 365
this code :
$date = date('Y-m-d');$date = date('Y-M-D');
the second i Think will show : 2014-Aug-Sun
-
09-13-2014, 07:02 PM #9Newbie
- Join Date
- Dec 2012
- Posts
- 14
Normally what I do is
date("Y-m-d", strtotime(now));
-
09-13-2014, 08:43 PM #10WHT Addict
- 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 slowerKomputer 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
-
09-14-2014, 11:25 AM #11Aspiring Evangelist
- Join Date
- Apr 2014
- Posts
- 365
date("Y-m-d", strtotime(now));
-
09-15-2014, 02:18 AM #12WHT Addict
- Join Date
- Sep 2007
- Posts
- 120
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
-
09-18-2014, 01:06 AM #13WHT Addict
- 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
-
[PHP] If this date! then New, else nothing.
By e-zone in forum Programming DiscussionReplies: 9Last Post: 07-15-2008, 10:26 AM -
help with php DATE problem
By zoya23 in forum Programming DiscussionReplies: 2Last Post: 01-25-2005, 05:24 AM -
why system date and php date are different
By e-view in forum Hosting Security and TechnologyReplies: 1Last Post: 10-08-2004, 03:28 AM -
PHP date and server date are different!
By vovanus in forum Programming DiscussionReplies: 2Last Post: 09-22-2004, 12:35 PM -
PHP Date...
By CreativeLogic in forum Programming DiscussionReplies: 8Last Post: 05-19-2004, 05:06 PM