Converting separate month, day and year values into a timestamp

前端 未结 7 762
不知归路
不知归路 2021-02-12 16:32

I have a month value (1-12), day value (1-31), and a year value (2010,2011,2012). I also have a hour value and a minute value.

How can I give this to strtotime()

7条回答
  •  甜味超标
    2021-02-12 17:23

    Given the variables $year $month $day $hour $minute you can do:

    strtotime("$year-$month-$day $hour:$minute");
    

    Be careful to enclose the variables with ", never in this case with '.

    UPDATE (thanks to comments of @Clockwork and @Tadeck):

    In case there is a variable $timeofday that represents the time of day (i.e. AM or PM), then you can parse it this with:

    strtotime("$year-$month-$day $hour:$minute$timeofday");
    

    that is to say, just attach that variable to the end of the text.

提交回复
热议问题