0

伙计们,我被困住了,我正在尝试使用我自己的格式将字符串转换为日期时间:

<?php
require('qs_connection.php');
require('qs_functions.php');

$sDay = $_POST['sDay']; 
$sMonth = $_POST['sMonth'];
$sYear = $_POST['sYear'];
$sHour = $_POST['sHour'];
$sMin = $_POST['sMin'];

$eDay = $_POST['eDay'];
$eMonth = $_POST['eMonth'];
$eYear = $_POST['eYear'];
$eHour =  $_POST['eHour'];
$eMin = $_POST['eMin'];

function num($n){
    $n=$n;
    if ($n<9){
        $n="0"."$n";
    }
    else{
        $n=$n;
    }
    return $n;
}
    function month($mt){
    switch($mt){
        case "Jan":
            return "01";
        case "Feb":
            return "02";
        case "Mar":
            return "03";
        case "Apr":
            return "04";
        case "May":
            return "05";
        case "Jun":
            return "06";
        case "Jul":
            return "07";
        case "Aug":
            return "08";
        case "Sep":
            return "09";
        case "Oct":
            return "10";
        case "Nov":
            return "11";
        case "Dec":
            return "12";
        break;
    }
}
    $startDate = $sYear."-".month($sMonth)."-".num($sDay)." ".num($sHour).":".num($sMin).":00";
    $endDate = $eYear."-".month($eMonth)."-".num($eDay)." ".num($eHour).":".num($eMin).":00";

    $startDate = date_create_from_format("Y-m-d H:i:s", $startDate);
    $endDate = date_create_from_format("Y-m-d H:i:s", $endDate);
    $today = date("Y-m-d H:i:s");





    if(($today >($endDate || $startDate)) || ($endDate < ($startDate || $toady))){
        echo "Can not save";
    }
    else{
        $result = mysql_query("Select votetimeid From votetime ORDER BY votetimeid DESC LIMIT 1");
            while ($db_field = mysql_fetch_assoc($result)){
                $id = $db_field['votetimeid'];
            }
            $id+=1;

    echo "Start: $startDate <br>Today: $today <br>End: $endDate";

    }
?>

我收到此错误:

Catchable fatal error: Object of class DateTime could not be converted 
to string in     C:\wamp\www\mvote\admin\settime.php on line 80
4

1 回答 1

2

而不是

echo "Start: $startDate <br>Today: $today <br>End: $endDate";

尝试

echo "Start: {$startDate->format('Y-m-d H:i')} <br>Today: $today <br>End: {$endDate->format('Y-m-d H:i')}";

或类似的东西。文档:http ://www.php.net/manual/en/datetime.format.php

另外,请注意$toady错字。

您似乎对布尔运算的工作方式有误解。if 语句应如下所示:

if($today > $endDate || $today > $startDate || $endDate < $startDate || $endDate < $today) {
于 2013-03-31T20:28:12.450 回答