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

时间:2011-08-09 18:24:57

标签: php javascript

我不是那么擅长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

3 个答案:

答案 0 :(得分:2)

您可能需要查看phpjs网站。他们的代码显示了如何在JS中完成大量的PHP函数。

具体来说:strtotimedate

答案 1 :(得分:0)

JS没有任何远离strtotime的东西。你必须自己确定“下周二”。一旦你有了,你可以使用.getTime()提取时间戳值,这将是自1970年1月1日以来的毫秒数。此值也可以作为参数反馈到新的日期对象中,因此您可以使用外部的简单数字进行日期数学运算,然后使用结果再次创建新的日期对象。

e.g。

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

一旦你得到了“下周二”的代码,剩下的就是微不足道了

答案 2 :(得分:0)

要到下一个星期二(最近的将来)获得毫秒:

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