How to Get 30 Days Prior to Current Date

How to get 30 days prior to current date?

Try using the excellent Datejs JavaScript date library (the original is no longer maintained so you may be interested in this actively maintained fork instead):

Date.today().add(-30).days(); // or...
Date.today().add({days:-30});

[Edit]

See also the excellent Moment.js JavaScript date library:

moment().subtract(30, 'days'); // or...
moment().add(-30, 'days');

Today's date -30 days in JavaScript

setDate() doesn't return a Date object, it returns the number of milliseconds since 1 January 1970 00:00:00 UTC. You need separate calls:

var date = new Date();
date.setDate(date.getDate() - 30);
var dateString = date.toISOString().split('T')[0]; // "2016-06-08"

Javascript get date 30 days ago

You'll instead want to use:

var priordate = new Date(new Date().setDate(beforedate.getDate()-30));

if you want it on one line. By using:

new Date().setDate(beforedate.getDate()-30);

you're returning the time since epoch (a number, not a date) and assigning it to priordate, but it's not a Date anymore and so does not have the getDate function.

Add 30 days to a Current date - JS

var date = new Date(); // Now
date.setDate(date.getDate() + 30); // Set now + 30 days as the new date
console.log(date);

how to get the 30 days before date from Todays Date

T-SQL

declare @thirtydaysago datetime
declare @now datetime
set @now = getdate()
set @thirtydaysago = dateadd(day,-30,@now)

select @now, @thirtydaysago

or more simply

select dateadd(day, -30, getdate())

(DATEADD on BOL/MSDN)

MYSQL

SELECT DATE_ADD(NOW(), INTERVAL -30 DAY)

( more DATE_ADD examples on ElectricToolbox.com)

Add 30 days to date

Please try this.

echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) . PHP_EOL;

This will return 06/06/2016. Am assuming your initial date was in m/d/Y format. If not, fret not and use this.

echo date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '05/06/2016')))) . PHP_EOL;

This will give you the date in d/m/Y format while also assuming your initial date was in d/m/Y format. Returns 05/07/2016

If the input date is going to be in mysql, you can perform this function on mysql directly, like this.

DATE_ADD(due_date, INTERVAL 1 MONTH);

Get 30 days back date along with time

The problem is likely caused by the malformed date() call. The first argument passed to date() should be the format (as shown in the Docs) and the second should be an optional timestamp.

Try this:

$d2 = date('c', strtotime('-30 days'));

PHPFiddle


As a short aside, the whole snippet can be simplified as follows:

$url = $row["url"];
$pageid = getPageID($url);
$date = date('y-m-d g:i');
$d1 = time();
$d2 = date('y-m-d g:i', strtotime('-30 days'));


Related Topics



Leave a reply



Submit