[英]Quick question about date formatting php/javascript
I'm failing a bit to get this working. 我有点无法使这个工作。
I read out a date from my database in the yyyy-mm-dd
format. 我从数据库中以
yyyy-mm-dd
格式读取了一个日期。 However, I need to use it in my jQuery ajax call in the dd-mm-yyyy
format. 但是,我需要在
dd-mm-yyyy
格式的jQuery ajax调用中使用它。 Is there a way to turn my dates around? 有没有办法改变我的约会? I should've seen this coming when I started working on my app, but alas, I didn't :(
当我开始使用我的应用程序时,我应该已经看到了这种情况,但是,可惜我没有:(
Don't feel like changing around the way I save stuff to my DB so I was wondering if anyone knows an easy way to change the format? 不想更改将内容保存到数据库的方式,所以我想知道是否有人知道更改格式的简便方法? Thanks :(
谢谢 :(
EDIT: Just ran into another, similar problem 编辑:刚遇到另一个类似的问题
I read time out as, for example, 08:00:00
I want to split this into parts aswell. 我读超时,例如
08:00:00
我也想将其分为几部分。 For example 例如
08:00:00 => var a = 8, var b = 00 // ignore seconds
09:30:00 => var a = 9, var b = 30
23:45:00 => var a = 23, var b = 45
10:30:00 => var a = 10, var b = 30
:( Thanks! :( 谢谢!
Format your date directly in your sql query : 直接在sql查询中设置日期格式:
SELECT DATE_FORMAT(fielddate,'%d-%m-%Y') as jsDate, DATE_FORMAT(fielddate,'%m-%d-%Y') as phpdate FROM xxx
You can do multiple format in the same query to fit your need (a format for js , one for php, one for direct display ...) 您可以在同一查询中执行多种格式以满足您的需要(一种格式用于js,一种格式用于php,一种用于直接显示...)
In PHP you can easily turn it around. 在PHP中,您可以轻松解决它。
$date=date('d-m-Y', strtotime('2009-11-12'));
You could also achieve this using Javascript: 您也可以使用Javascript实现:
var date='2009-11-12'.split('-').reverse().join('-');
EDIT concerning your update: 编辑有关您的更新:
var timeArray=myTime.split(':');
is what you need here. 这就是您需要的。 It grabs a string, and returns a normal array with the elements of the string splitted by
:
. 它抓取一个字符串,并返回一个普通数组,其中的字符串元素由
:
分隔。 So timeArray[0]
will be the hour, timeArray[1]
the minute. 所以
timeArray[0]
将是小时, timeArray[1]
是分钟。
Yes, you can turn it around, but yyyy-mm-dd is the internationally accepted way to represent dates in computers, so you really should not. 是的,您可以改变它,但是yyyy-mm-dd是国际上公认的在计算机中表示日期的方式,因此您实际上不应该这样做。
Instead, you should change your database, and if you want to present the date to the user in another format, you do the conversion for the presentation only. 相反,您应该更改数据库,并且如果您想以另一种格式向用户显示日期,则仅对显示进行转换。
EDIT: Sorry if this answer sounds rude, but I really believe that you will thank me later if you do this. 编辑:对不起,如果这个答案听起来很粗鲁,但我真的相信,如果您这样做,以后会感谢我。 Or at least keep it in mind until next time :)
或者至少要记住,直到下次:)
使用日期功能:
date("d-m-Y",strtotime($yourdate));
<?php
$newdate = date('d-m-Y',strtotime($db_date))
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.