Separate the zero of the dates

1

asked by anonymous 02.06.2017 / 21:56

3 answers

3

Do the return conversion to integer type:

$mesAtual = (int) date("m");

In this way, var_dump will result in:

int(6)

If you prefer, you can use the intval

$mesAtual = intval(date("m"));

This function has a second parameter that defines the numerical basis of the result. By default it is 10 and so in this case it does not have to be specified. A basic test using microtimes differences indicates that doing only the type cast is faster than calling the function (in Ideone it showed / strong> faster).

define("START", microtime(TRUE));

$mesAtual = intval(date("m"));

define("MIDDLE", microtime(TRUE));

$mesAtual = (int) date("m");

define("END", microtime(TRUE));

echo "intval: ", MIDDLE - START, PHP_EOL;
echo "type cast: ", END - MIDDLE, PHP_EOL;

Result:

intval: 0.043507099151611
type cast: 3.814697265625E-6
  

See working at Ideone .

    
02.06.2017 / 22:00
4

Just use n instead of m , according to the documentation :

  

Numeric representation of a month, without zero left

Therefore:

$mesAtual = date('n');
var_dump($mesAtual);

// = string(1) "6"

Try this.

It will be enough, so the representation will be from 1 to 12, instead of 01 to 12 .

    
02.06.2017 / 23:34
0

Using Regular Expression example - ideone

$stt = strftime('%m', strtotime("06/04/2017")); 

echo preg_replace('/0(\d)/', '$1', $stt);
    
04.06.2017 / 01:54