convert each dd mm yy to unix time and insert in birthday_date field

和自甴很熟 提交于 2019-12-12 01:48:19

问题


please i want to make a nice php function that convert my html input values into a unixTime stamp and than insert to my mysql table field

This is my HTML code

<input type="text" name="birthday_dd"/>
<input type="text" name="birthday_mm"/>
<input type="text" name="birthday_yy"/>

i have a lots of this html fields in my form each one termin with _dd & _mm & _yy so i want to merge this 3 html input values and convert them into a unix timeStamp before insert into my Mysql Table field


回答1:


To get a UNIX timestamp from the date parts, you can use mktime()

$timestamp = mktime(0, 0, 0, $month, $day, $year)

By the way, you should take advantage of the DATE and DATETIME field types of MySQL instead of storing unix timestamps.




回答2:


Use mktime as stated previously or make them a timestamp with UNIX_TIMESTAMP('YYYY-MM-DD') when you issue your INSERT query !




回答3:


It seems you are about to handle birthday. UNIX timestamps might not be the right tool here, because it gets tricky when someone is born before the UNIX era. I would recommend you, to use the appropriate database types instead. In case of MySQL you'll find the documentation here.




回答4:


Use strtotime method,

e.g. $unix = strtotime("2014-11-10");


来源:https://stackoverflow.com/questions/22505786/convert-each-dd-mm-yy-to-unix-time-and-insert-in-birthday-date-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!