简体   繁体   English

在date()中减去n值

[英]Substracting the n value in date()

I want to implement a Javascript countdown timer that has the month value substracted by 1. 我想实现一个Javascript倒计时器,它的月值减去1。

To get the date dynamically via PHP I use this code: 要通过PHP动态获取日期,请使用以下代码:

$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: 我的问题是如何将值n减去1,因此最终输出将始终如下:

2014, (3-1), 9, 00

Here's the DateTime() way to do it (I used dashes instead of colons as that is the proper separator for date parts): 这是DateTime()方法(我使用破折号而不是冒号,因为这是日期部分的正确分隔符):

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

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 . 2014, 1, 19, 00将是2013, 12, 19, 00但不是2014, 0, 19, 00


Update: 更新:

You want to pass a date to the jQuery plugin( jquery.magicbusmultimedia.net ). 您想将日期传递给jQuery插件( jquery.magicbusmultimedia.net )。

The plugin only ask you to pass a javascript Date object. 该插件只会要求您传递一个javascript Date对象。

So you could do: 所以你可以这样做:

 $('#myCounter').mbComingsoon(new Date(<?php echo strtotime($date); ?> * 1000)); 

$date = "2014-3-19 00:00:00"; $ date =“2014-3-19 00:00:00”; $newDate = date("Y, n, j, H, i", strtotime('-1 month', strtotime($date))); $ newDate = date(“Y,n,j,H,i”,strtotime(' - 1个月',strtotime($ date))); And 2014, 1, 19, 00 will be 2013, 12, 19, 00 but not 2014, 0, 19, 00. 2014,1,19,00将是2013年,12日,19日,但不是2014年,0日,19日。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM