0

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 like 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

3
  • You use str_replace function. I'll let you get some practice on your own so look it up at php.net and try it yourself. Jun 3, 2011 at 13:28
  • 2
    Did you even check the manual? :/
    – Salman
    Jun 3, 2011 at 13:29
  • 1
    PHP: date - Manual
    – Midas
    Jun 3, 2011 at 13:30

5 Answers 5

7

You can just do:

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

Why don't you use

$todaydate = date('Ymd');
1

Possibility when dealing with strings you could use

preg_replace('/-|:/', null, $date);
1
  • If you're going this way. Just do a str_replace(). preg_replace() is slower.
    – PeeHaa
    Jun 3, 2011 at 13:40
0

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

0

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'),
));
3
  • why do the str_replace call when its built into date?
    – locrizak
    Jun 3, 2011 at 13:30
  • 1
    Why the downvote? Perhaps he's using the date time in that format somewhere else in his code?
    – PeeHaa
    Jun 3, 2011 at 13:31
  • true.. just sounded doubtful from the request
    – locrizak
    Jun 3, 2011 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.