Php: DateTime: how to set only seconds (not hours neither minutes)
Asked Answered
C

6

20

I have a code where I have to "round" to the lowest minute.

  • 16:05:00 should become 16:05:00
  • 16:05:01 should become 16:05:00
  • 16:05:29 should become 16:05:00
  • 16:05:30 should become 16:05:00
  • 16:05:31 should become 16:05:00
  • 16:05:59 should become 16:05:00

I want to use the DateTime object. There are no functions such as:

  • setHours()
  • setMinutes()
  • setSeconds()

Here's the beginning of my code:

$my_date=DateTime::createFromFormat(
    'Y-m-d H:i:s',
    $bd_date
);  
$my_date->setTimezone(self::$TimeZone);

Now I'd like to set the "seconds" part to zero.

Do you have an elegant way of only setting minutes the way I'd like to?

Nota: I'm not looking for a solution like "divide by getTime() by 60, convert to integer, then multiply by 60". I'm looking for a generic solution to set the seconds, not only to "0".

Comitia answered 27/6, 2012 at 10:6 Comment(0)
W
49

Is setTime elegant?

$my_date->setTime ( $my_date->format("H"), $my_date->format("i"), $new_second_val );
$my_date->setTime ( $my_date->format("H"), $new_minute_val, $new_second_val );
// etc...
Weatherspoon answered 27/6, 2012 at 10:17 Comment(9)
it is THE most efficient way to SET hours and minutes on a DateTime object. Also, format is the only documented way to extract hours from a DateTime.Weatherspoon
NO, it is not elegant. Carbon is elegant. Thank you for your answer.Zantos
Ok. Carbon, however, is not in the PHP standard library, so while it may be more elegant, it's not quite what OP asked for.Weatherspoon
The OP never said that the third party libraries is off limit for the project. And it was provocative of you to ask "Is it elegant?", when it is obviously not. Don't get me wrong, I like your solution, it is really good for cripple projects, where no third party libraries are allowed.Zantos
I want to use the DateTime object.Weatherspoon
Note: if you have declare(strict_types=1) , you need to add an explicit cast to int , i.e (int) $my_date->format("H") etc.Higley
@Higley The answer was written way before (int) was a thing ;)Weatherspoon
@Weatherspoon yes , my comment was more for the guy in 2020 (i.e me) who will see this answer and wonder why it throws an exception.Higley
not so elegent, but its readable and worked solution ! really !Karenkarena
A
12

Just set the seconds to "00"

date_default_timezone_set('America/Sao_paulo');
$date = new DateTime();
echo $date->format('Y-m-d H:i:00');
Apgar answered 27/6, 2012 at 10:19 Comment(2)
I tried but it doesn't work... returns false instead of an object.Comitia
This doesn't work, if you use createFromFormat(), since the given value is 2017-02-15 15:10:20 . So the seconds aren't parsed and causing function not to return a DateTime objectResting
P
3

Best way to set the second/minute or hour by simply using the second/minute or hour function on Carbon DateTime Object. (obvious you will need to create carbon object)

\Carbon\Carbon::now()->hour(2)->minute(20)->second(0);
Penholder answered 3/4, 2019 at 6:32 Comment(1)
When I read that (even though it's perfect and good Php code) I'm so glad I've given up on Php 3 years ago ;^)Comitia
E
2

I know this is a late answer but it might help someone the way it helped me.

You can add the first line if you need a specific timezone

date_default_timezone_set('Africa/Johannesburg'); $created = date('Y-m-d H:i:00', time());

Encephalic answered 27/2, 2019 at 19:1 Comment(0)
C
0

from docs should be

$dt = Carbon::create(2012, 1, 31, 15, 32, 45);
echo $dt->startOfMinute(); // 2012-01-31 15:32:00
Coccidioidomycosis answered 10/1, 2020 at 23:40 Comment(0)
V
0
$db = new DateTime();
$db->setTime($db->format('H'), $db->format('i'), 0);
$currentMinute = $db->format('Y-m-d H:i:s');
Villainy answered 14/6 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.