Nrc
Nrc

Reputation: 9797

PHP time, include the first day

I have a timestamp in a MySql database. The format is: 2018-08-28 12:39:59

I want to select different periods of time. I have problems with the time part. For instance:

$begin = strtotime("first day of this month midnight");
$begin = date("Y-m-d h:i:s", $begin);

If I echo $begin it gives me: 2018-08-01 12:00:00
So, it does not includes for instance: 2018-08-01 01:00:00
I have been searching and the official documentation says:

'midnight' The time is set to 00:00:00

http://php.net/manual/en/datetime.formats.relative.php

How to include the whole first day in the begin variable? (begin from 0 or ignore time)

Upvotes: 0

Views: 33

Answers (2)

Javier Larroulet
Javier Larroulet

Reputation: 3237

PHP's date function has two different modifiers: H and h h displays hour in a 12-hour format (so midnight is 12... if you added the AM/PM modifier you'd see 12:00:00 am)

try

$begin = date("Y-m-d H:i:s", $begin);

The will output 00:00:00 as you expect

Upvotes: 1

AymDev
AymDev

Reputation: 7609

You are using the wrong hour format, you should do:

//     uppercase H --v
$begin = date("Y-m-d H:i:s", $begin);

// Output: 2018-08-01 00:00:00
echo $begin;

From the documentation:

  • h: 12-hour format of an hour with leading zeros
  • H: 24-hour format of an hour with leading zeros

Upvotes: 1

Related Questions