从 Mysql 检索日期并将其溢出到特定列表
我正在尝试从 Mysql 数据库检索日期到我的 html 表单以编辑记录,现在我用来将日期插入数据库的方式是从列表中获取年-月-日,然后将它们统一在一个变量中像这样:
$dob = $_POST['Year'] . '-' . $_POST['Month'] . '-' .$_POST['Day'];
这将在我的表中插入像这样格式0000-00-00的$dob
的值,
问题是我将如何检索这个变量并将其每个元素拆分到我的特定列表
中尝试过似乎无用且错误这段代码
$row['userDateOfBirth'] = $year . '-' . $month . '-' . $day ;
然后将每个变量放入(例如:年份)
<option value="1920" selected="selected"><? echo $year ; ?></option>
这不起作用,我该怎么做?
i am trying to retrieve date from Mysql db to my html form in order to edit the records, now the way i used to insert the date into the database was getting Year - Month - Day each from a list and then unite them in one variable like this :
$dob = $_POST['Year'] . '-' . $_POST['Month'] . '-' .$_POST['Day'];
this will insert the value of $dob
like this format 0000-00-00 in my table
the problem is that how i will retrieve this variable and split it each element to its specific list
what i tried seems useless and wrong which is this code
$row['userDateOfBirth'] = $year . '-' . $month . '-' . $day ;
then put each variable to (example:year)
<option value="1920" selected="selected"><? echo $year ; ?></option>
this did not work , how can i do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您有一个实际的日期/日期时间字段,您可以在 MySQL 中进行拆分:
这将为您提供三个包含各个日期组件的单独字段。您还可以在 PHP 中执行
explode('-', $datestr)
之类的操作,将 'yyyy-mm-dd' 分解为单独的 yyyy、mm 和 dd 块。有很多选项,您可以选择最简单/最适合您的特定问题的选项。
Assuming you've got an actual date/datetime field, you can do the splitting inside MySQL:
which gives you three separate fields containing the individual date components. You could also do things like
explode('-', $datestr)
in PHP to decompose 'yyyy-mm-dd' into individual yyyy, mm, and dd chunks.Lots of options, it's up to you to pick which one is easiest/best for your particular problem.
您可以在客户端(php)端处理它。
如果您的日期无效,$year 将是 1970。
You can handle that on the client (php) side.
if you have an invalid date, $year will have 1970.
您可以使用
Where 基本上告诉date() 函数返回传递给它的时间的格式化字符串,在本例中是通过strtotime() 创建的。
Date() 参考。上面的示例返回“2000-01-01”中的日期,请参阅参考资料来为您的项目选择适当的格式。
You could use
Where basically you're telling the date() function to return a formatted string of the time passed to it, in this case created via strtotime().
Date() reference. The above example returns the date in "2000-01-01", see the reference for selecting the appropriate format for your project.