Faizan Rupani Faizan Rupani - 9 days ago 6
PHP Question

Unix timestamp for today at specific time

I am using this code to get current unix time in php.

echo time();


and it is giving me this result

1480578383


If I convert this in readable date then it will be

Thu, 01 Dec 2016 07:46:23 GMT


Well I don't want that. What I want is a function that can give me today's date with specific time which is 23:00:00.

And when I convert it to readable date then it should be

Thu, 01 Dec 2016 23:00:00 GMT


"Thu, 01 Dec 2016" is the current date.

Answer

I'll make the assumption that the spec is:

Get today at 23:00:00 in current time zone

Code would look like this:

mktime(23, 0, 0);

... or:

strtotime('today 23:00:00')

Test code:

$time_zones = array(
    'America/Chicago',
    'GMT',
    'Europe/Berlin',
);
foreach($time_zones as $time_zone){
    date_default_timezone_set($time_zone);
    $t1 = mktime(23, 0, 0);
    $t2 = strtotime('today 23:00:00');
    echo date('r', $t1) . ' / ' . date('r', $t2) . PHP_EOL;
}
Thu, 01 Dec 2016 23:00:00 -0600 / Thu, 01 Dec 2016 23:00:00 -0600
Thu, 01 Dec 2016 23:00:00 +0000 / Thu, 01 Dec 2016 23:00:00 +0000
Thu, 01 Dec 2016 23:00:00 +0100 / Thu, 01 Dec 2016 23:00:00 +0100

Please find further details at mktime() and strtotime() manual pages.