0

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
000
  • 26,951
  • 10
  • 71
  • 101
maponda1
  • 48
  • 1
  • 1
  • 8

1 Answers1

2

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) {
000
  • 26,951
  • 10
  • 71
  • 101
  • That big checkmark to the left of the answer could use some love :) – 000 Mar 31 '13 at 20:38
  • the if else part is not working, how can i write it correctly? – maponda1 Mar 31 '13 at 23:04
  • thanx it worked, maybe you can help me on this other question: http://stackoverflow.com/questions/15798917/opera-mobile-need-to-zoom – maponda1 Apr 03 '13 at 22:54
  • i mean the one in this link: http://stackoverflow.com/questions/15798917/why-are-my-web-pages-zoomed-in-when-i-open-them-in-opera-mobile – maponda1 Apr 03 '13 at 23:54
  • 1
    You just linked the same one twice. And I already answered it. – 000 Apr 03 '13 at 23:57