質問

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
役に立ちましたか?

解決 2

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

他のヒント

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

$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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top