在另一个特定时间之后获取特定日期的 Unix 时间戳
Get Unix timestamp of specific date after another specific time
我可以使用以下代码获取特定日期的 3 月 19 日:
$date = strtotime(" 19 March", $current_time);
例如,如果我给出 2010 年 1 月 1 日的 unix 时间戳作为输入,它给了我 2010 年 3 月 19 日。但是如果我给出 2010 年 3 月 20 日的 unix 时间戳,我仍然得到 19 March 2010. 我想要的是下一个 3 月 19 日,在这种情况下,它将是 2011 年 3 月 19 日。
我怎样才能做到这一点?
您应该首先从指定日期获得 year
。然后你可以创建 19 march
日期和年份并使用 strtotime()
获取时间戳。
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
使用 PHP DateTime 可以实现如下:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
你可以像这样
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
您可以指定要在一天中增加或减少多少天或一周,以及使用这些功能设置时间
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();
我可以使用以下代码获取特定日期的 3 月 19 日:
$date = strtotime(" 19 March", $current_time);
例如,如果我给出 2010 年 1 月 1 日的 unix 时间戳作为输入,它给了我 2010 年 3 月 19 日。但是如果我给出 2010 年 3 月 20 日的 unix 时间戳,我仍然得到 19 March 2010. 我想要的是下一个 3 月 19 日,在这种情况下,它将是 2011 年 3 月 19 日。 我怎样才能做到这一点?
您应该首先从指定日期获得 year
。然后你可以创建 19 march
日期和年份并使用 strtotime()
获取时间戳。
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
使用 PHP DateTime 可以实现如下:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
你可以像这样
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
您可以指定要在一天中增加或减少多少天或一周,以及使用这些功能设置时间
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();