PHP 在字符串中添加冒号
您好,我有这个字符串,它将生成然后插入到数据库中的时间
$time=mktime(date('G'),date('i'),date('s'));
$NowisTime=date('Gis',$time);
现在我需要将冒号重新添加到该字符串中: :
我需要生成它并在没有冒号的情况下插入它,但在另一个中显示重新插入冒号的地方,这样它看起来像这样:
13:24:09
而不是:
132409
$todaydate = date('Ymd');
也是如此 应该变成 2011-06-16< /代码>
我怎样才能做到这一点?
计算单词数并不好,因为我们可以根据实际时间和日期拥有更多或更少的类型。
请指教
谢谢!
对于不理解的人:这个值是从数据库中获取的,所以我不能使用:date('Ymd');从数据库中获取的值......
Hello I have this string that will generate the time that I then insert in the db
$time=mktime(date('G'),date('i'),date('s'));
$NowisTime=date('Gis',$time);
Now I need to readd colons to this string: :
I need it to be generated and inserted without colons, but shown in another place with colons reinserted, so that it will look like this:
13:24:09
Instead of:
132409
The same is for $todaydate = date('Ymd');
That should become then 2011-06-16
How can I do that?
Counting the words is not good, since we can have more or less types depending by the actual time and date.
Please advise
Thank you!
FOR WHO DOES NOT UNDERSTAND: this values are taken from the DB so I cannot use : date('Y-m-d'); in a values taken from the db........
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对于这一点,请尝试:
与您的其他部分类似。
编辑 ::
对于日期:
$time =“数据库中的时间”;
$unixtimestamp = strtotime ($time);
$date = date('Ymd', $unixtimestamp);
对于时间:
For this one, try :
Similar for your Other part.
Edit ::
For Date :
$time = "time from database";
$unixtimestamp = strtotime ( $time) ;
$date = date('Y-m-d', $unixtimestamp);
For time :
您提供的用于格式化的字符串可以包含您想要的任何内容,因此放置破折号或分号没有问题。您甚至可以包含其他文本,只要日期代码中使用的任何字母都用反斜杠转义即可。
整个内容
也可以重写。您正在获取当前时间的时间戳,然后将其提供给
date()
。date
默认使用当前时间,因此不需要这样做来显示当前时间。这是一种方法:
The strings you supply for formatting can contain anything you'd like, so putting the dashes or semicolons there is no problem. You can even include other text, as long as any letters used in the date code are escaped with a backslash.
The entire
could be rewritten, too. You're maing a timestamp from the current time, then giving it to
date()
.date
uses the current time by default, so there's no need to do that to show the current time.Here's one way:
如果您完全按照您所说的做,您可以从末尾开始计算字符,并在正确的位置添加字符(
:
和-
):两者
date('is')
和date('Ymd')
使用前导零生成固定格式,因此长度始终相同。您只需补偿date('G')
部分。因此,实际上您所要做的就是从字符串末尾删除 2 个字符两次,剩下的就是年份或小时。
If you are doing exactly what you say you are doing, you can count the characters starting at the end and add the characters (
:
and-
) at the right place:Both
date('is')
anddate('Ymd')
produce a fixed format using leading zeros so the length is always the same. You only have to compensate for thedate('G')
part.So really all you have to do is chop off 2 characters from the end of the string twice and what remains is the year or the hour.
那么,如果您使用
date('his')
代替并将其转换为字符串(因此 PHP 不会将其解释为整数并删除可能的前导零),您可以通过拆分来添加冒号每两个数字串起来,然后用冒号将其内爆。对于第二部分,执行相同的操作,只不过将字符串分割为 4 个字符,然后将第二个分割分割为 2 个字符。
但是,在这两种情况下,从格式化字符串(带有冒号和破折号)开始并将其删除会更容易对于数据库。
Well if you use
date('his')
instead and cast it as a string (so PHP doesn't interpret it as an integer and remove the possible leading zero), you can add colons by splitting the string every two numbers and then imploding it with colons.For the second part, do the same except split the string by 4 characters and then split the second split by 2.
In both situations however it would just be easier to start out with the formatted strings (with the colons and dashes) and remove them for the db.
当您生成要存储在数据库中的值时,请使用 Unix 时间戳:
这样您根本不必担心它的外观或解析它。然后,当您准备好向用户显示它时,您可以使用:
它将以冒号(这甚至是一个单词吗?)格式为您的用户显示它。
When you are generating the value to store in your database, use a Unix timestamp:
That way you don't have to worry about how it looks or parsing it at all. Then, when you're ready to display it to your user, you can use:
which will display it in the colonated (is that even a word?) format for your users.
你不能做这样的事情:
Couldn't you do something like :