943,632 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1529
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 20th, 2009
0

Format datetime to use with mysql

Expand Post »
I need to format PHP to use with one of my date field (DATETIME).
I want to get current time and format it. I have checked some googled articles but I cannot get far.
Please help me!
Similar Threads
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Sep 20th, 2009
-1

Re: Format datetime to use with mysql

In php use:

PHP Syntax (Toggle Plain Text)
  1. $mydate = date('Y-m-d H:i:s');
  2. //this will produce a date like 2009-09-20 17:44:09
  3.  
  4. $query = mysql_query("INSERT INTO mytable SET mydatefield = '{$mydate}', field2 = 'value2'");
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 943
Sarcastic Poster
ardav is offline Offline
6,671 posts
since Oct 2006
Sep 20th, 2009
1

Re: Format datetime to use with mysql

Dont format the date time in the database
text formatted dates are for humans to read, sql is not human does not need any of that and it makes the processing slower
sql Date, php date, unix timestamp, are all a single 10byte numeric that stores complete date and time
formatting is done for human readability on output
example <?php echo date('DMY h:m',$timestamp} ?> selecting a timestamp renage from num1 to num2 is much faster - less processing than searching text dates for dates between x and 1
sql now() will input the date and time of the update as the update is processing
php date() is now

1253454900 (10bytes)
or
September 20 2009, 1:55pm (25bytes)

not much on 1 row, very much on a million rows

select * from table where date > 1234567890 and date < 1234568890
is much faster than precessing text dates
processing script languages have strtotime() (or an identical function) built in, you dont even have to do any text to time conversions on range select fields

text date time: not good
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Sep 20th, 2009
-1

Re: Format datetime to use with mysql

I'd agree with AB about now() in SQL - better than using date() first and then using it, but if you need the current date for output to screen as well as input to db, you may as well.
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 943
Sarcastic Poster
ardav is offline Offline
6,671 posts
since Oct 2006
Sep 21st, 2009
0

Re: Format datetime to use with mysql

Click to Expand / Collapse  Quote originally posted by almostbob ...
Dont format the date time in the database
text formatted dates are for humans to read, sql is not human does not need any of that and it makes the processing slower
sql Date, php date, unix timestamp, are all a single 10byte numeric that stores complete date and time
formatting is done for human readability on output
example <?php echo date('DMY h:m',$timestamp} ?> selecting a timestamp renage from num1 to num2 is much faster - less processing than searching text dates for dates between x and 1
sql now() will input the date and time of the update as the update is processing
php date() is now

1253454900 (10bytes)
or
September 20 2009, 1:55pm (25bytes)

not much on 1 row, very much on a million rows

select * from table where date > 1234567890 and date < 1234568890
is much faster than precessing text dates
processing script languages have strtotime() (or an identical function) built in, you dont even have to do any text to time conversions on range select fields

text date time: not good
Thanks for explanations. Can you please give example of how to use it?
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Sep 21st, 2009
0

Re: Format datetime to use with mysql

For Complete Date & time Statement in PHP with MySQL Please Go to:
http://in2.php.net/manual/en/function.date.php

The above will give u all your problem solution.
Thank You..
Last edited by hemgoyal_1990; Sep 21st, 2009 at 3:56 am.
Reputation Points: 18
Solved Threads: 17
Junior Poster
hemgoyal_1990 is offline Offline
175 posts
since Aug 2007
Sep 21st, 2009
0

Re: Format datetime to use with mysql

For Complete Date & time Statement in PHP with MySQL Please Go to:
http://in2.php.net/manual/en/function.date.php

The above will give u all your problem solution.
Thank You..
Thanks, I'll check!
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Sep 21st, 2009
0

Re: Format datetime to use with mysql

Click to Expand / Collapse  Quote originally posted by evstevemd ...
Thanks, I'll check!
Welcome Dude..
Reputation Points: 18
Solved Threads: 17
Junior Poster
hemgoyal_1990 is offline Offline
175 posts
since Aug 2007
Sep 21st, 2009
0

Re: Format datetime to use with mysql

I solved it with gmdate()
php Syntax (Toggle Plain Text)
  1. function getnow(){
  2. //mysql date format -- 'YYYY-MM-DD HH:MM:SS'
  3. // Create the UNIX Timestamp, using the current system time
  4. $ptime = time();
  5. // Convert that UNIX Timestamp into a string (GMT), safe for MySql -- GMT +3
  6. $toffset = +3.00;
  7. $stime = gmdate("Y-m-d H:i:s", $ptime+$toffset * 3600);
  8. return $stime ;
  9. }
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Sep 21st, 2009
0

Re: Format datetime to use with mysql

No dude(dudess?)
store the 10 digit timestamp in the database
convert to text on output
Mysql does not care what it looks like and it is faster smaller and simpler for mysql to process the numeric to search for records between 1 January and 1 March
the returned $time function is much larger than 10bytes
that function would be better in the script used output data to be human readable in reports
Store $ptime
$ptime 1234568888
$stime Monday 13 Feb 2009 10:55am
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Email with attachment corrupts file
Next Thread in PHP Forum Timeline: How to protect my videos from downloading





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC