-3

I am parsing the string "11:00 PM" to the database. I want to convert the string to military time as 23:00 and write to the database.

I tried with the below:

$traveltime = "11:00 PM";
$contraveltime = date(strtotime("%H:%i",'$traveltime')); 

I get the error

A PHP Error was encountered
Severity: Warning
Message: date() expects parameter 2 to be long, string given
Filename: views/addtrip_individual.php
Line Number: 332

Line 332 is where the second code line is.

How do I make this work?

cmnardi
  • 1,051
  • 1
  • 13
  • 27
user396123
  • 59
  • 1
  • 2
  • 12

5 Answers5

5

The format argument belongs in the date function, not in strtotime. And you need to eliminate the single quotes around $traveltime, or you'll just be evaluating the literal string '$traveltime'. Also, the % characters shouldn't be used in the format string. Those are used for printf, etc. and aren't required here.

$contraveltime = date("H:i", strtotime($traveltime));
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
2

Please start with looking at the date manual. One simple way, that I would use is:

echo date("G:i", strtotime("3:00 PM"));   // 15:00

Since 1500 is a military time, not 15:00, you might need to do:

echo date("Gi", strtotime("3:00 PM"));    // 1500
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
1

you can do some think like

// 24-hour time to 12-hour time 
$time_in_12_hour_format  = date("g:i a", strtotime("13:30"));

// 12-hour time to 24-hour time 
$time_in_24_hour_format  = date("H:i", strtotime("1:30 PM"));

Ref link :How do you convert between 12 hour time and 24 hour time in PHP?

Community
  • 1
  • 1
Vision Coderz
  • 8,257
  • 5
  • 41
  • 57
0

maybe the error is just the way you call the functions try this way

$traveltime = "11:00 PM";
$contraveltime = date('H:i', strtotime($traveltime));
cmnardi
  • 1,051
  • 1
  • 13
  • 27
0

Its show error because date function first parameter must date format

but you given strtotime function

for correct result please follow below code

$traveltime = "11:00 PM";
$contraveltime = date('H:i',strtotime($traveltime));
echo $contraveltime;

and out put will be like below code

23:00

now you store $contraveltime into database

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Akbor
  • 1,280
  • 1
  • 9
  • 11