datesub soru

3 Cevap php

bu gibi date_sub kullanmak mümkün mü?

$dt = date("Y-m-d h:i:s");

$query = "INSERT INTO `table` (`date`) VALUES ('DATE_SUB('$dt', INTERVAL 15 DAY)')";

$result = MYSQL_QUERY($query);

Teşekkürler

3 Cevap

You could use it on database by removing the quotes around DATE_SUB(...). But you should consider using the unix time stamp with time() and reconverting it to a date format using the date() function, like this:

$dt = date("Y-m-d h:i:s", time() - 15*24*3600); //today - 15 days

PHP 4 ve PHP 5 ile çalışır.

Ben veritabanına takvim hesaplamaları bırakmamaya tavsiye ediyorum. Yerine DateTime class kullanın ve timezonedb uzantısı güncellenmiş. Bu aslında PHP doğru olur böyle şeyler biridir.

this post derick blogunda da bakınız.

Bu 'DATE_SUB('$dt', INTERVAL 15 DAY)', bir dize var yanlıştır.

Şimdi, tarih biçimi MySQL'in NOW() fonksiyonu tarafından kullanılan biçimi aynıdır.

SELECT NOW();
---------------------
|now()              |
---------------------
|2010-05-15 20:42:35|
---------------------

Ve ben yeni bir projede DATE_SUB(NOW(), INTERVAL 6 MONTH); herhangi bir sorun olmadan kullanarak oldum beri, ben bunu kullanmak değil herhangi bir neden görmüyorum.

SELECT DATE_SUB( NOW( ) , INTERVAL 6 MONTH );
-----------------------------------
|DATE_SUB(NOW(), INTERVAL 6 MONTH)|
-----------------------------------
|2009-11-15 20:49:28              |
-----------------------------------