Jump to content

subtract a month from a date()


Paul-D

Recommended Posts

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”

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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');  
?>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.