doulai6469 2013-03-31 20:23
浏览 48
已采纳

将时间转换为字符串[关闭]

Guys i am stuck I am trying to convert a String to date time using my own Format:

<?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";

    }
?>

I am getting this error:

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

1条回答 默认 最新

  • duanliaolan6178 2013-03-31 20:28
    关注

    Rather than

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

    Try

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

    or something similar. Documentation: http://www.php.net/manual/en/datetime.format.php

    Also, notice the $toady typo.

    You seem to have a misunderstanding of how boolean operations work. The if statement should look like this:

    if($today > $endDate || $today > $startDate || $endDate < $startDate || $endDate < $today) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题