Hi I have been able to extract a VARCHAR to a date using string_to_date, however whatever I try the date always echoes as e.g. 2009-05-25
Here is my code that works:

您好我已经能够使用string_to_date将VARCHAR提取到日期,但是无论我尝试使用哪个日期,总是会回复到例如2009-05-25这是我的代码:

$query = "SELECT u.url_id, url, title, description, STR_TO_DATE( pub_date, '%d-%b-%Y') 
    AS pub_date FROM urls AS u, url_associations AS ua WHERE u.url_id = ua.url_id AND
    ua.url_category_id=$type AND ua.approved = 'Y' ORDER BY pub_date DESC";
$result = mysql_query ($query);
echo "  <tr>
<td align=\"left\">{$row['pub_date']}</td>
</tr>\n";

I have tried DATE_FORMAT and similar methods but I either receive 2009-05-25 or blank. Can anyone help me solve this issue. I am looking and testing but I have decided to ask for help here as the assistance is welcomed and those who helped previously were very kind.

我尝试了DATE_FORMAT和类似的方法,但我收到2009-05-25或空白。任何人都可以帮我解决这个问题。我正在寻找和测试,但我决定在这里寻求帮助,因为欢迎提供帮助,之前帮助过的人非常友善。

Thanks

Sean

2 个解决方案

#1


The return type of STR_TO_DATE is DATE which is returned to PHP.

STR_TO_DATE的返回类型是DATE,它返回给PHP。

It's PHP that formats dates in your echo, not MySQL.

这是PHP在回声中格式化日期,而不是MySQL。

To do the formatting on MySQL side, use:

要在MySQL端进行格式化,请使用:

DATE_FORMAT(STR_TO_DATE( pub_date, '%d-%b-%Y'), '%Y.%m.%d')

or other format.

或其他格式。

The inner format controls how your string is expected to be stored in the database, the outer format controls how will it be output.

内部格式控制着如何将字符串存储在数据库中,外部格式控制如何输出。

To do the formatting on PHP side (which is better as you can honor culture specific formats for different users), use:

要在PHP端进行格式化(这样可以更好地为不同用户提供特定于文化的格式),请使用:

echo date('Y m d', strtotime($row['pub_date']))

更多相关文章

  1. 更新日期字段时为空 - MySQL PHP
  2. 使用date_default_timezone_set和日期的可能的PHP bug ?
  3. 关于PHP 读取EXCEL时间(不是日期)的问题
  4. Laravel 5验证日期为php Y格式。g 2015 ?
  5. 将纪元时间转换为日期PHP
  6. 如何减去两个日期和时间来得到不同?
  7. 以完整二叉树,数组格式获取所有节点
  8. php执行数据库查询返回json格式数据
  9. 在PHP中如何取得两个日期时间相减的结果,得出大于一天,大于二天,大

随机推荐

  1. 关于Android环境配置SDK的问题Failed to
  2. 第一次写博客,先上传一下平常开发android
  3. Android 源码修改按键长按响应间隔
  4. Android之屏幕切换使用技巧
  5. Android 中屏幕点击事件的实现
  6. 【Android】WebView设置背景色
  7. android volley ,多文件 表单上传
  8. Android Studio v0.1尝鲜
  9. android 如何依赖android:sharedUserId更
  10. Android入门篇