如何将php时间函数转换为javascript函数

How to turn php time function into javascript function?

本文关键字:函数 转换 javascript 时间 php      更新时间:2023-09-26

我不是很擅长javascript(尚未),所以我需要一些帮助,与这个php脚本的替代版本(在javascript)

function until($format = ""){
    $now = strtotime("now");
    $nextTuesday = strtotime("-1 hour next tuesday");
    $until = $nextTuesday - $now;
    if(empty($format)){
        return $until;
    }else{
        return date("$format",$until);
    }
}

只是需要它倒数,直到下周二,在一个非常短的方式(不是在20+行,像我看到的所有其他脚本)它仍然应该返回一个时间戳,如果可能的话(离线应用需要它)

所以如果有人能帮助我,我会很高兴(不是说我现在不高兴,但我会更高兴):D

您可能想看看phpjs站点。他们的代码展示了如何在JS中完成大量PHP函数。

具体为:strtotimedate

JS没有任何接近strtotime的东西。你得自己决定"下周二"。一旦获得了这些,就可以使用. gettime()提取时间戳值,这将是自1970年1月1日以来的毫秒数。这个值也可以作为参数反馈到一个新的date对象中,这样您就可以在外部使用简单的数字进行日期数学运算,然后使用结果再次创建一个新的date对象。

var now = new Date();
var ts = now.getTime();
var next_week = ts + (86400 * 7 * 1000);
next_week_object = new Date(next_week);

一旦你弄清楚了"下周二"的代码,剩下的就很简单了

获取距离下星期二(最近的将来)的毫秒数:

function f_until(){
  var now = new Date(Date.now());
  var nextT = new Date(Date.now());
  var cD = nextT.getDay();
  if(cD < 2)nextT.setDate(nextT.getDate() + (2-cD));
  else nextT.setDate(nextT.getDate() + (9-cD));
  nextT.setHours(nextT.getHours() - 1);
  //alert('next tuesday: '+nextT.toString()); 
  return nextT.getTime() - now.getTime();
}