Is it faster to use the mysql query:
SELECT CURDATE() as today
or the PHP statement:
$curdate = date('Y-m-d');
Does the same answer apply to using date()
VS MySQL's NOW()
and CURTIME()
?
If you're simply doing the query to get a date into a PHP script, then use the PHP function. In both cases, PHP and MySQL will call the system clock, mangle that time value into the formatted string, and return it. The MySQL version will have the added overhead of query parsing/compiling, and the roundtrip from PHP->MySQL->PHP. And if you're talking to MySQL via TCP socket, you've got added overhead of building/sending/receiving a TCP connection and packets.
On the other hand, if you're using that data value within a query, you'd be better off keeping it within MySQL. For instance, by random chance, you may start a script immediately before midnight, so that PHP generates "May 24/2011 23:59:59.999" for its timestamp, but the query doesn't execute in MySQL until "May 25/2011 00:00:00.001", so now the dates don't match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With