strtotime总是返回01-1-1970

时间:2016-05-17 12:20:48

标签: php html datepicker

我真的尝试了一切,我从日期选择器中得到了这样的日期:05/10/2016

然后我用/替换了 - 。使用strtotime但我总是得到01-1-1970。

这是我的代码:

 if (isset($_POST['CampaignName']) && isset($_POST['CampaginBudget']) && isset($_POST['start_date']) && isset($_POST['end_date']) && $email  )
 {
   // receiving the post params
    $CampaignName= $_POST['CampaignName'];
    $CampaignBudget = $_POST['CampaginBudget'];
    $start_date= str_replace('/', '-', $_POST['start_date']);
    $end_date = explode('/', $end_date);
    $start_date = date('Y-m-d', strtotime($start_date));
    $end_date = date('Y-m-d', strtotime(implode('-', strtotime($end_date))));
    //$end_date = date('Y-m-d', strtotime($end_date2));
    echo $start_date;  echo $end_date;

编辑:startdate工作正常,但结束日期有问题。即使它们完全相同

html代码:

Campaign Duration :<input type="text" name="start_date" class="form-control" id="datetimepicker" placeholder="mm/dd/yyyy"/  required> <span> TO </span>  <input type="text" name="end_date" class="form-control" id="datetimepicker2" placeholder="mm/dd/yyyy"/   required>

2 个答案:

答案 0 :(得分:1)

你试过这个吗?

$originalDate = $_POST['start_date'];
echo $newDate = date("Y-m-d", strtotime($originalDate));

答案 1 :(得分:0)

你在结束日期的双倍时间上做了stint strtotime,这就是为什么会出现问题:

结束日期只需:

$end_date = explode('/', $end_date);
$end_date = date('Y-m-d', strtotime(implode('-', $end_date))); // remove strtotime from implode function

完整代码:

if (isset($_POST['CampaignName']) && isset($_POST['CampaginBudget']) && isset($_POST['start_date']) && isset($_POST['end_date']) && $email  )
 {
   // receiving the post params
    $CampaignName= $_POST['CampaignName'];
    $CampaignBudget = $_POST['CampaginBudget'];
    $start_date= str_replace('/', '-', $_POST['start_date']);
    $start_date = date('Y-m-d', strtotime($start_date));
    $end_date = $_POST['end_date']; // add this line
    $end_date = explode('/', $end_date);
    $end_date = date('Y-m-d', strtotime(implode('-', $end_date))); // remove strtotime from implode function
    echo $start_date;  echo $end_date;