aslum
aslum

Reputation: 12254

best way to strip a leading 0 from numerical representation of a month?

So I have a PHP variable w/ a (possibly the current, possibly not) month in it in the form of ##. If the month is less then October I want to take the leading 0 out. Is there a good and simple way to do this?

Upvotes: 0

Views: 6933

Answers (5)

Murat Kurbanov
Murat Kurbanov

Reputation: 837

you can do it like:

<?php
      $month = intval(Date('m'));
      echo $month; // prints int value
    ?>

Upvotes: 0

pankaj
pankaj

Reputation: 1906

$today_mem = date("j.n"); 

Use j instead of d and n instead of m:

source:: https://www.php.net/manual/en/function.date.php

Upvotes: 1

Wh1T3h4Ck5
Wh1T3h4Ck5

Reputation: 8509

<?php

  $month = '05'; // May
  $month *= 1;

  echo $month; 

?>

Output

5

Upvotes: 1

bmbaeb
bmbaeb

Reputation: 540

You can use the built in date function to do this, should look something like

<?php
date('n/d/Y',strtotime($dateVariable));
?>

You can use n instead of m to display the month without the leading zeros.

See http://php.net/manual/en/function.date.php for more information about date formatting.

Upvotes: 8

John Conde
John Conde

Reputation: 219804

Cast it as an integer:

<?php
  $month = '01';
  echo (int) $month; // prints 1
?>

Upvotes: 8

Related Questions