Convert PHP time

Asked

Viewed 795 times

1

I intend to convert the time I receive that comes from an input time (01:00) to the "his" format I want the result to be 010000. I used this code, it worked but notice error.

$hour=date("his",$txthourstart);

How can I convert to this format?

  • You used strtotime() to convert the value?

  • No, you have to convert?

  • It is recommended because direct string assignment will cause errors in data interpretation.

  • I used $hora=strtotime($txthourstart); but if I have 23:00, put it to 110000. I wanted the result to be 230000.

  • Then use "His". The "H" character represents the format in 24 hours, while the "h" represents it in 12.

1 answer

1


Prefer to use the methods and classes provided by PHP to use the interpretation of temporal data. There are two main solutions:

Object Oriented (>= PHP 5.2)

$hourStart = DateTime::createFromFormat("H:i", $txthourstart);
$hour = $hourStart->format('His');

Procedural

$hour = date("His", strtotime($txthourstart));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.