4

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.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 1
    What do you mean with specific time? The example you gave is same with the example you don't want? Kindly clarify. Thanks! – Rav Dec 01 '16 at 08:10
  • If it's 23:00 at your location, fix the clock of the unix machine. If it's 7:46 at your location, the function works well. If you want the current date with any other time you can do `(time()/86400)*86400` to strip the time and then add i.e. `23*60*60`. – Holger Dec 01 '16 at 08:13
  • `echo (new DateTime("23:00"))->format('r');` – Charlotte Dunois Dec 01 '16 at 08:29
  • 1
    @Holger What you suggest does not work. Math does not work like that. The prefered method is using `DateTime`. – Charlotte Dunois Dec 01 '16 at 08:30

1 Answers1

3

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')

... or just:

strtotime('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');
    $t3 = strtotime('23:00:00');
    echo date('r', $t1) . ' / ' . date('r', $t2) . ' / ' . date('r', $t3) . PHP_EOL;
}
Fri, 02 Dec 2016 23:00:00 -0600 / Fri, 02 Dec 2016 23:00:00 -0600 / Fri, 02 Dec 2016 23:00:00 -0600
Fri, 02 Dec 2016 23:00:00 +0000 / Fri, 02 Dec 2016 23:00:00 +0000 / Fri, 02 Dec 2016 23:00:00 +0000
Fri, 02 Dec 2016 23:00:00 +0100 / Fri, 02 Dec 2016 23:00:00 +0100 / Fri, 02 Dec 2016 23:00:00 +0100

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

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • I could also use this but for it to work. I must use date_default_timezone_set function to set timezone. Is there a way to work around to get proper date without using date_default_timezone_set function. – Faizan Anwer Ali Rupani Dec 01 '16 at 09:27
  • This is just a way to **test that code works properly**. You don't have to run the test every time you use the code. You can just copy and paste the first line of code and ignore everything bellow the "Test code" sentence. – Álvaro González Dec 01 '16 at 09:29
  • for me it's not working properly. It's showing 22 hours in place of 23 hours – Faizan Anwer Ali Rupani Dec 01 '16 at 09:31
  • And if you don't care about time zone your code will run with the system's default time zone. This is probably fine if you purchase hosting in a local company. – Álvaro González Dec 01 '16 at 09:31
  • which is faster mktime or strtotime in this situation? – Faizan Anwer Ali Rupani Dec 01 '16 at 09:34
  • I can speculate that `strtotime()` has an additional parsing phase so it cannot be faster but I'm sure you'd need to do it several million times to appreciate any difference. (As about the 22h issue, could you create an [online demo](https://3v4l.org/) that reproduces the issue?) – Álvaro González Dec 01 '16 at 09:47
  • No. on phpfiddle it's working fine. but not on localhost. – Faizan Anwer Ali Rupani Dec 01 '16 at 09:50