Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a php date object current date without seconds

Tags:

php

I need a date object that has a time of 12:00:00am for the current day (meaning no seconds). I am converting that to that to the number of seconds and passing it in another function. It is eventually used for a report filter using date = "someDateHere' off the database, and the hanging seconds in the field are screwing up the report.

I'm not sure what to put in the second parameter in the time function - leaving it blank will use the current time, which is what I do not want. I can't find examples or anything in the php doc. If there is another function that will do the job, I am open to suggestions. This should be simple, but it is alluding me.

        date_default_timezone_set('America/Detroit');
        $now = date("Y-m-d 0:0:0");
        echo $now . '<br/>';
        $now = time($now,0);
        echo $now . '<br/>';

Thanks in advance.

edit: Please note: I need to convert that date object to seconds. That is where the timestamp is screwing me up with strtotime function and time function. Even though I am passing it a dateobject without a timestamp, converting it into seconds not-so-conveniently is inserting the timestamp as the second parameter which defaults to the current time.

like image 346
Cymbals Avatar asked Mar 21 '13 21:03

Cymbals


3 Answers

There are lots of available options here, since PHP accepts a wide variety of time formats.

$midnight = strtotime('midnight');
$midnight = strtotime('today');
$midnight = strtotime('12:00am');
$midnight = strtotime('00:00');
// etc.

Or in DateTime form:

$midnight = new DateTime('midnight');
$midnight = new DateTime('today');
$midnight = new DateTime('12:00am');
$midnight = new DateTime('00:00');
// etc.

See time formats and relative formats in the manual for a complete list of formats with descriptions.

like image 172
salathe Avatar answered Nov 03 '22 05:11

salathe


Oh, I'd stop using those functions entirely, and start taking advantage of the DateTime class!

$date = new DateTime("now", new DateTimeZone("America/Detroit"));
echo $date->format("Y-m-d");

http://php.net/manual/en/class.datetime.php

like image 25
Thomas Kelley Avatar answered Nov 03 '22 07:11

Thomas Kelley


time() takes no arguments. what you're doing is pointless. why not just strtotime(date('Y-m-d')) to get the unix timestamp for midnight?

like image 44
Marc B Avatar answered Nov 03 '22 06:11

Marc B