1

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!

2
  • 1
    Why are you mixing DateTime with strtotime()? Why are you even using strtotime() at all? Commented Apr 12, 2019 at 1:46
  • Fixed. echo strtotime($dataformat,'18:30:00'); Should be echo strtotime($datafmt. '18:30:00'); That is my error. Now code return 1555018200 : ) John I learning php, probably I do a lot of incorrect things on the road :) I can showme better way of do what this code do, I really appreciate in order to learn. Commented Apr 12, 2019 at 1:51

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.