大家好,我用来strtotime
转换日期,但我面临的问题是它将时间设置为 19:00:00,这是错误的。我正在发送一个日期字符串,然后将其提供给strtotime,它设置了时间本身。现在我的时间是 11:58,但它将时间存储为 19:00。请告诉我为什么我遇到此错误。代码如下:strtotime($s_date)
$s_date
仅包含一个日期字符串strtotime
用于设置时间
我发送的日期字符串是这个03/04/2016
.HTML
<input type="text" class="form-control" id="starts" data-container="#addNewEvent"
data-plugin="datepicker">
阿贾克斯调用
$.ajax({
type: "POST",
url: base_url + "apps/calendar/insertCalendar",
async : false,
dataType: 'html',
data: {
'start': $('#starts').val(),
'end': $('#ends').val(),
'title': $('#addTitle').val(),
'description': $('#addDescription').val(),
'type':type
},
success: function(mark_up){
toastr.options = {positionClass: 'toast-top-center'};
toastr.success('Your reminder has been created', 'Reminder created');
window.location.reload();
}
});
控制器方法
public function insertCalendar(){
$s_date = $_POST["start"];
$p = strtotime($s_date);
error_log($p)
}
还有一件事我在 mongodb 中抚摸它,所以我从 strtotime 获得的对象将它们转换为new MongoDate(strtotime($s_date))
首先我们需要设置第一个timezone
date_default_timezone_set('Asia/Karachi');
然后,您可以使用以下代码获取时间和日期:
echo date('Y-m-d h:i:s'); /* Current date and time*/
echo date('Y-m-d'); /* Current date */
echo date('h:i:s'); /* Current time */
如果要将日期转换为所需的格式。例如。 03/04/2016
,所以使用
$s_date = '03/04/2016';
echo date('Y-m-d',strtotime($s_date)); /* Convert date into mysql date format */
您将根据 mysql 的格式获得日期Y-m-d
例如 2016-03-04
.
现在您希望用户提供的日期与当前时间,那么您应该使用
$s_date = '03/04/2016'.' '.date('h:i:s');
echo date('Y-m-d',strtotime($s_date));
这将解决您的问题。