我不知道为什么这么困惑,也许是因为用mysql和php输出/输入日期和时间的方法太多了。我只想让用户以这种格式输入日期
MM/DD/YYYY
并以这种格式输出
Month Name - Date
对于时间,我希望用户从选择字段中选择时间,从下午 1:00 开始。并递减 30 分钟,所以它将是下午 1:30、下午 2:00...我想以某种方式将其插入 MySQL 并将其输出为下午 1:30。也不知道该怎么做。
最佳答案
在数据库中存储日期格式并能够以您想要的方式显示它的最佳方法是使用 time()
和 date()
函数.
当在你的数据库中存储日期时,你应该使用 time(),它会生成一个像这样的字符串 -> 1300695900
。这个小字符串包含日期和时间
然后您可以使用日期以任何您想要的方式显示它:
$time = time();//you would normally get this from the database
date('d M Y', $time); //outputs 21 Mar 2011
date('m-d-y', $time); // outpus 03-21-2011
等等……
编辑: 要回答你的最后一个问题,你只需获得不同的值,将它们粘在一起(连接)然后使用 strtotime()
$date = $_POST['date']; // eg 03/03/2011
$time = $_POST['time']; // eg 1:30
$daypart = $_POST['daypart']; // eg PM
$date_time = $date.' '.$time.' '.$daypart;
$strtime = strtotime($date_time);
echo date('d M Y - h:i', $strtime);// outputs 03 Mar 2011 - 01:30
关于php - 使用 PHP 在 mysql 中插入/输出日期和时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5374925/