开发者

PHP remove dots and hyphens

Hello I need to remove dots and hyphens from both variables time and date.

This is my code:

$todaydate = date('Y-m-d') ."\n";
$time_utc=mktime(date('G'),date('i'),date('s'));
$NowisTime=date('G:i:s',$time_utc);

That will return something li开发者_如何学Cke this:

$todaydate = 2011-06-03
$NowisTime = 14:20:30

What I need to achieve is:

$todaydate = 20110603
    $NowisTime = 142030

How do I do this?

Thank you


You can just do:

$todaydate = date('Ymd');
$time_utc=mktime(date('G'),date('i'),date('s'));
$NowisTime=date('Gis',$time_utc);


Why don't you use

$todaydate = date('Ymd');


Possibility when dealing with strings you could use

preg_replace('/-|:/', null, $date);


Don't type the hyphens and dots at all in your date() calls!


In your case I would just use PHP's DateTime class:

$dateTime= new DateTime('now');
var_dump(array(
    'currentDate' => $dateTime->format('Ymd'),
    'currentTime' => $dateTime->format('Gis'),
));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜