4

I want to implement a Javascript countdown timer that has the month value substracted by 1.

To get the date dynamically via PHP I use this code:

$date = "2014:3:19 00:00:00";
$newDate = date("Y, n, j, H, i", strtotime($date));

Which returns:

2014, 3, 9, 00

My question is how can I substract the value n by 1, so the final output will be always like this:

2014, (3-1), 9, 00
John Conde
  • 217,595
  • 99
  • 455
  • 496
zoora
  • 65
  • 2
  • 8

3 Answers3

3

Here's the DateTime() way to do it (I used dashes instead of colons as that is the proper separator for date parts):

$date = "2014-3-19 00:00:00";
$date = (new DateTime($date))->modify('-1 month')->format("Y, n, j, H, i");

or

$date = "2014-3-19 00:00:00";
$date = (new DateTime($date))->diff(new DateInterval('P1M'))->format("Y, n, j, H, i");
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

If you mean minus one month, then you could do:

$date = "2014-3-19 00:00:00";
$newDate = date("Y, n, j, H, i", strtotime('-1 month', strtotime($date)));

And 2014, 1, 19, 00 will be 2013, 12, 19, 00 but not 2014, 0, 19, 00.


Update:

You want to pass a date to the jQuery plugin(jquery.magicbusmultimedia.net).

The plugin only ask you to pass a javascript Date object.

So you could do:

$('#myCounter').mbComingsoon(new Date(<?php echo strtotime($date); ?> * 1000));
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

$date = "2014-3-19 00:00:00"; $newDate = date("Y, n, j, H, i", strtotime('-1 month', strtotime($date))); And 2014, 1, 19, 00 will be 2013, 12, 19, 00 but not 2014, 0, 19, 00.