首页 > 解决方案 > 将在 ajax 调用中工作的 Strtotime 函数

问题描述

我正在尝试使用 php 中的 strtotime 连接来自多个选择框的日期和时间值,但是,我需要在我的 ajax 调用中使用这个转换后的时间戳变量。

编码:

    <form>
    <select id="month">
        <option value="1">January</option>
    </select>
    <select id="day">
        <option value="22">22</option>
    </select>
    <select id="year">
        <option value="2020">2020</option>
    </select>
    <select id="time">
        <option value="12:00:00">12:00</option>
    </select>
    <button id="submitNow">
</form>

$("#submitNow").click(function(e){ 
        e.preventDefault();

        var timestamp = /*need to basically do strtotime($month . "-" . $day . "-" . $year . " " .  $time) */

        var promises = [];
        var promise;


        $.when.apply($, promises).then(function() {
            $.ajax({
               type:'POST',
               url:'url/test',
               data:{timestamp:timestamp},
               _token: '{{ csrf_token() }}',
               success:function(data){  
                  window.location.href = "/library";
               }
          });
    });
});

所以基本上,在 PHP 中,我可以转储strtotime($month . "-" . $day . "-" . $year . " " . $time)并获取 1-22-2020 12:00:00 的 unix 时间戳,但我需要以某种方式(在 laravel 中)这样做,以便我可以在我的 ajax 中提交时间戳(1548158400)称呼

我能做到这一点的最好方法是什么?

标签: javascriptphplaravelstrtotime

解决方案


Blade 仍然只是一个 php 文件(name.blade.php),所以你应该能够做到这一点。

var timestamp = <?php echo strtotime($month . "-" . $day . "-" . $year . " " .  $time); ?>

推荐阅读