kkh
kkh

Reputation: 4869

PHP datetime value not passing in as a argument

I am using the DateTime object in php to get a range of dates

However I am unable to pass in the value of the DateTime object into a function.

When i try assigning it to a variable, the variable is empty

Code as belows

$date1 = new DateTime('2013-04-11');
$test = $date1->setTime($i, 59);
echo $test;

Anyone any idea how do I assign the $date1 to a variable?

Upvotes: 0

Views: 3172

Answers (3)

PeeHaa
PeeHaa

Reputation: 72692

That returns a DateTime object which doesn't have a toString method.

The above will generate a fatal error (you should really enable errors):

Catchable fatal error: Object of class DateTime could not be converted to string in /code/KHCgOS on line 7

PHP Catchable fatal error: Object of class DateTime could not be converted to string in /code/KHCgOS on line 7

You should do something like:

echo $test->format('H:i:s d-m-Y');

More info: http://codepad.viper-7.com/TNHGlj

Note: on a production machine you should also have error reporting enabled, but instead of displaying them you should log them

Upvotes: 3

Rolando Isidoro
Rolando Isidoro

Reputation: 5114

From the DateTime::setTime function documentation, it returns a DateTime object, so in order to print the value you have to use the DateTime::format function. Here's an working example:

<?php
$i = 14;

$date1 = new DateTime('2013-04-11');
$date1->setTime($i, 59);

echo $date1->format('Y-m-d H:i:s') . "\n";

returns

2013-04-11 14:59:00

Upvotes: 0

ryder
ryder

Reputation: 51

use http://www.php.net/manual/en/datetime.format.php

   $date1 = new DateTime('2013-04-11');
   $date1->setTime($i, 59);
   echo $date1->format('Y-m-d H:i:s');

Upvotes: 0

Related Questions