如何使用PHP中的date_create_from_format函数将动态数据用作参数

I have string naming the date and time in the format of d-m-Y_h-i-sa

an example being '10-01-2019_03-31-06pm'

I want to turn this into a unix timestamp.

So I use $date = date_create_from_format('d-m-Y_h-i-sa', '10-01-2019_03-31-06pm')

and then echo date_timestamp_get($date)

This works, but the problem is that I won't always be passing through literal strings for the second parameter of the function.. as instead I want to use data from a table in a database so that it's dynamic.

an example being $row['file_name']

However if I do $date = date_create_from_format('d-m-Y_h-i-sa', $row['file_name'])

and then echo date_timestamp_get($date)

it doesn't echo anything.

I have done var_dump($row['file_name']) and it confirms it is definitely a string.. so I don't understand why the first way works (using a literal string) but the second way won't work (when that's also classed as a string)

In $date = date_create_from_format('d-m-Y_h-i-sa', '$row['file_name']') there should be no quotes around $row['file_name] because doing so gives this as a string, not the value. So instead do

$date = date_create_from_format('d-m-Y_h-i-sa', $row['file_name'])