Paul-D Posted January 27, 2019 Share Posted January 27, 2019 (edited) Hi if i have $date = "12-08-2018" or "2018-08-12" Need to subtract a month. Could this be done for day and year even? TIA Desmond. P.S. What I want to do is clear out a database table with records over a month old so today is 27th Jan 2018. I would like to create a date “2018-12-01” I can then use this in an SQL to delete all before “2018-12-01” Edited January 27, 2019 by Paul-D Quote Link to comment Share on other sites More sharing options...
Barand Posted January 27, 2019 Share Posted January 27, 2019 $date = new DateTime('first day of last month'); $stmt = $pdo->prepare("DELETE FROM mytable WHERE recorddate < ?"); $stmt->execute( [$date->format(Y-m-d)] ); Make sure dates stored in your database are in the correct yyyy-mm-dd format Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 27, 2019 Author Share Posted January 27, 2019 The problem with this is, it is a PHP website I have created. So <?php $date = new DateTime('first day of last month'); echo $date; ?> doesn't work. I subscribe to a website company easyspace. Do I need to do anything to create the DateTime class? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 27, 2019 Share Posted January 27, 2019 That IS PHP code so why is it a problem?. (Unless you are using a php version prior to 5.2!) Try <?php $date = new DateTime('first day of last month'); echo $date->format('Y-m-d'); ?> Do you have error reporting turned on? Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 27, 2019 Author Share Posted January 27, 2019 echo PHP_VERSION; = 5.4.45 Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 27, 2019 Author Share Posted January 27, 2019 (edited) <?php echo PHP_VERSION; error_reporting(E_ALL); ini_set('display_errors', 1); $date = new DateTime('first day of last month'); echo $date; ?> Edited January 27, 2019 by Paul-D Quote Link to comment Share on other sites More sharing options...
Barand Posted January 27, 2019 Share Posted January 27, 2019 Did echoing using the $date->format() method work? Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 27, 2019 Author Share Posted January 27, 2019 I am used to C++ and visual C++ There you have to include a class structure into the code. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 27, 2019 Share Posted January 27, 2019 5 minutes ago, Barand said: Did echoing using the $date->format() method work? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 27, 2019 Share Posted January 27, 2019 check output from phpinfo(). Is date support enabled? Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 27, 2019 Author Share Posted January 27, 2019 (edited) Fatal error: Call to undefined function phpinfo() Edited January 27, 2019 by Paul-D Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 27, 2019 Author Share Posted January 27, 2019 (edited) <?php echo PHP_VERSION; error_reporting(E_ALL); ini_set('display_errors', 1); echo phpinfo(); //$date = new DateTime('first day of last month'); //echo $date; ?> Edited January 27, 2019 by Paul-D phpinfo() returns bool Quote Link to comment Share on other sites More sharing options...
Barand Posted January 27, 2019 Share Posted January 27, 2019 6 minutes ago, Paul-D said: Fatal error: Call to undefined function phpinfo() Sounds like your php installation is seriously FUBAR Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 28, 2019 Author Share Posted January 28, 2019 its not my php. I RENT web space from a company Easy Space (UK). I don't have direct access to their server only to upload and download to my space and I also have a MySql database.. If there are any questions I should ask them then please tell me. TIA Desmond. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 28, 2019 Share Posted January 28, 2019 If it's shared hosting it could be that they have disabled phpinfo() for security reasons. DateTime should be available though with your version of php (even though it is several years old). You still haven't told me if the amendment to your code that I posted is working. (You are still posting "echo $date") On 1/27/2019 at 1:37 PM, Barand said: Try <?php $date = new DateTime('first day of last month'); echo $date->format('Y-m-d'); ?> Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 29, 2019 Author Share Posted January 29, 2019 Using your code supplied I get Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.' in /vhost/vhost15/d/e/s/desmond-otoole.co.uk/www/class.php:6 Stack trace: #0 /vhost/vhost15/d/e/s/desmond-otoole.co.uk/www/class.php(6): DateTime->__construct('first day of la...') #1 {main} thrown in /vhost/vhost15/d/e/s/desmond-otoole.co.uk/www/class.php on line 6 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 29, 2019 Share Posted January 29, 2019 Your default timezone should be set in your php.ini file (see example below) Alternatively, you need to set it in your program before call ing any datetime functions EG date_default_timezone_set("Europe/London"); Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 29, 2019 Author Share Posted January 29, 2019 I have never used a php.ini file and don't know how it is implemented. Can I not use $date = new DateTime('first day of last month', "Europe/London"); or do I just create a file php.ini in the www root of my website with the lines [Date] date.timezone=Europe/London Quote Link to comment Share on other sites More sharing options...
imgrooot Posted January 29, 2019 Share Posted January 29, 2019 On 1/27/2019 at 6:20 AM, Paul-D said: Hi if i have $date = "12-08-2018" or "2018-08-12" Need to subtract a month. Could this be done for day and year even? TIA Desmond. P.S. What I want to do is clear out a database table with records over a month old so today is 27th Jan 2018. I would like to create a date “2018-12-01” I can then use this in an SQL to delete all before “2018-12-01” Here's how I would do it. // GET THE DATE FROM YOUR DATABASE TABLE $db_date = '12-08-2018'; // SUBTRACT DAYS FROM IT $get_date = DateTime::createFromFormat('Y-m-d H:i:s',$db_date); $get_date->modify('-30 days'); $new_date = $get_date->format('Y-m-d H:i:s'); // NEW DATE WILL BE YOUR ORIGINAL DATE MINUS 30 DAYS echo $new_date; Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 29, 2019 Author Share Posted January 29, 2019 I have created a php.ini file and added it to the website root contents [Date] date.timezone=Europe/London Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 29, 2019 Author Share Posted January 29, 2019 $get_date->modify('-30 days'); I could just as well do $date = time() $diff = 60*60*24*30; $date = $date - $diff; However if it was the 31st of the month this would not take me to the previous month. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 29, 2019 Share Posted January 29, 2019 (edited) On 1/27/2019 at 11:20 AM, Paul-D said: I can then use this in an SQL to delete all before “2018-12-01” The problem you stated was that you had a date of 27th January and want to delete those before the start of the previous month. That is not the same as just subtracting 1 month or 30 days. Which is it? [edit] if you just want to delete everything older than 1 month you don't need php DELETE FROM mytable WHERE recorddate < CURDATE() - INTERVAL 1 MONTH Edited January 29, 2019 by Barand Quote Link to comment Share on other sites More sharing options...
gizmola Posted January 29, 2019 Share Posted January 29, 2019 Strongly agree with Barand on this. I blogged about this topic a mere 14 years ago, but you might find it to a helpful elaboration. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 29, 2019 Share Posted January 29, 2019 I always find it a lot easier and more intuitive to just use something like CURDATE() + INTERVAL 5 DAY rather than using the DATE_ADD() and DATE_SUB() functions Quote Link to comment Share on other sites More sharing options...
Paul-D Posted January 29, 2019 Author Share Posted January 29, 2019 would CURDATE() + INTERVAL 5 DAY take me back to the first of the previous month. In this case 1st December 2018 ? ? ? ? ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.