Jump to content
Paul-D

subtract a month from a date()

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”

Edited by Paul-D

Share this post


Link to post
Share on other sites
$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

Share this post


Link to post
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?

 

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

<?php
echo PHP_VERSION;
error_reporting(E_ALL);
ini_set('display_errors', 1);

$date = new DateTime('first day of last month');
echo $date;
?>
 

Edited by Paul-D

Share this post


Link to post
Share on other sites

Did echoing using the $date->format() method work?

Share this post


Link to post
Share on other sites

I am used to C++ and visual C++

There you have to include a class structure into the code.

Share this post


Link to post
Share on other sites
5 minutes ago, Barand said:

Did echoing using the $date->format() method work?

 

Share this post


Link to post
Share on other sites

check output from phpinfo(). Is date support enabled?

 

Capture.PNG

Share this post


Link to post
Share on other sites

Fatal error: Call to undefined function phpinfo() 

Edited by Paul-D

Share this post


Link to post
Share on other sites

<?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 by Paul-D
phpinfo() returns bool

Share this post


Link to post
Share on other sites
6 minutes ago, Paul-D said:

Fatal error: Call to undefined function phpinfo() 

Sounds like your php installation is seriously FUBAR

Share this post


Link to post
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.

 

Share this post


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

 

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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");

 

Capture.PNG

Share this post


Link to post
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

 

Share this post


Link to post
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;

 

Share this post


Link to post
Share on other sites

I have created a php.ini file and added it to the website root contents

[Date]
date.timezone=Europe/London

Share this post


Link to post
Share on other sites
$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.

Share this post


Link to post
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

Edited by Barand

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

would CURDATE() + INTERVAL 5 DAY take me back to the first of the previous month. In this case 1st December 2018 ? ? ? ? ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.