dongrang9300 2013-09-16 22:54
浏览 61
已采纳

当前日期无法在PHP中格式化

I'm using the jquery datepicker, and when today's date is selected, it's failing to convert to the format I need for a database query.

The datepicker date format is 'm-d-Y', so today appears as 09-16-2013. This part is working, and the datepicker correctly displays today's date as the default selection.

However, I need to use 'Y-m-d' format to query the database. This works fine when the date is less than today. But for today, it's failing and I'm getting 1969-12-31.

echo $enddt;

displays: 09-16-2013

I have tried:

echo date('Y-m-d', strtodate($enddt));
echo date('Y-m-d H:i:s', strtodate($enddt));

and neither works. How do I format $enddt so it displays 2013-09-16 ?

  • 写回答

2条回答 默认 最新

  • douliao8318 2013-09-16 22:57
    关注

    The function is actually strtotime(), not strtodate():

    Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

    Since you have the date string separated by dashes, we'll need to convert it into slashes first to make strtotime() recognize the format as mm/dd/yyyy:

    $enddt = '09-16-2013';
    $enddt = str_replace('-', '/', $enddt);
    echo date('Y-m-d', strtotime($enddt));
    

    Demo!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同