dmcoding dmcoding - 5 months ago 12
PHP Question

Trying to get hour 00:00:00 of a specific day, getting relative time instead

To be very brief, I'm trying to get a timestamp of a specific day (the beginning of the month) at a very specific time (00:00:00). I'm then trying to get that value into a unix time stamp used to search the database. The day is working fine, but when I check the time stamp it's setting itself to a relative time to the current time. For example, right now it is 8:55 am here and the output I'm receiving is 1:56 pm (UTC). I want 00:00 UTC. Here's the code I'm working with.

$from = "$month/1/$year";
$to = $month+1."/1/$year";
$fromDate = date_create("$from");
$toDate = date_create("$to");
$fromDate->setTime(0,0,0);
$toDate->setTime(0,0,0);
$from = strtotime($fromDate);
$to = strtotime($toDate);


here's the output:

$from = 1464789100

$to = 1467381099

edit: solved

My tables were messed up. I had some messed up data that was throwing everything off that I admittedly caused for testing purposes (because forgetting that your testing cases exist is always a good idea). Thanks all for your help.

Answer

Try this code

$date = DateTime::createFromFormat('m/d/Y H:i:s', '05/02/2015 00:00:00');
var_dump($date->format('U'));
/* Add 1 month to date */
$date->add(new DateInterval('P1M'));
var_dump($date->format('U'));
Comments