简体   繁体   中英

Php unix timestamp with strtotime

I start learn PHP and is very clear I make some error here, I try obtain Unix timestamp of specific hour and minutes of the day:

 <?php
 date_default_timezone_set('America/Argentina/Buenos_Aires');
 $data = new DateTime();
 $datafmt = $data->format('Y-m-d');
 echo strtotime($datafmt,'18:30:00');
 ?>

The code return 1554951600 and is equal to:

GMT: Thursday, April 11, 2019 3:00:00 AM
Your time zone: Thursday, April 11, 2019 12:00:00 AM GMT-03:00

This is wrong, timestamp should be:
1555018200 is equal to:

GMT: Thursday, April 11, 2019 9:30:00 PM
Your time zone: Thursday, April 11, 2019 6:30:00 PM GMT-03:00

What I doing wrong?


Fixed!

echo strtotime($datafmt. '18:30:00');   

, instead . that is my error!

You do not need strtotime() at all. DateTime class is a replacement and is more powerful. Just pass the time to the constructor or set it with the method setTime()

<?php
date_default_timezone_set('America/Argentina/Buenos_Aires');
$data = new DateTime('18:30:00');
// Alternative ways to set the time of the DateTime object
// $data->setTime('18', '30', '00');
// $data->setTime(...explode(':', '18:30:00'));
$datafmt = $data->format('U'); // U means UNIX timestamp
echo $datafmt;

I start learn PHP and is very clear I make some error here, I try obtain Unix timestamp of specific hour and minutes of the day:

 <?php
 date_default_timezone_set('America/Argentina/Buenos_Aires');
 $data = new DateTime();
 $datafmt = $data->format('Y-m-d');
 echo strtotime($datafmt,'18:30:00');
 ?>

The code return 1554951600 and is equal to:

GMT: Thursday, April 11, 2019 3:00:00 AM
Your time zone: Thursday, April 11, 2019 12:00:00 AM GMT-03:00

This is wrong, timestamp should be:
1555018200 is equal to:

GMT: Thursday, April 11, 2019 9:30:00 PM
Your time zone: Thursday, April 11, 2019 6:30:00 PM GMT-03:00

What I doing wrong?


Fixed!

echo strtotime($datafmt. '18:30:00');   

, instead . that is my error!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM