1

I am generating a reference number, using jQuery and Php comprising: a static string, a dynamic letter of the alphabet, the date in Month and Year and an autogenerated id.

I have these four elements concatenated as below.

$("#refno").text("ABC" + str + <?php echo date('Y') ?>  + <?php echo date('m')?>  + <?php echo $id -> id + 1; ?>);

str is the dynamic letter , set by selecting from a dropdown. It is required that I output the month in the mm format e.g 09 for September.

Challenge is, the leading zero in the month is truncated in the resulting ref number. e.g instead of ABCR20120923 i get ABCR2012923 . Yet when I echo date('m') 09 is output.

While I could choose to have an if statement that concatenates a zero onto the single character months, I would want to understand why zero is truncated and how I can have it displayed.

Thanks.Help Appreciated.

watkib
  • 347
  • 3
  • 11
  • 25

3 Answers3

3

09 on its own in treated like an integer - a number does not make sense with leading zeros.

$("#refno").text("ABC" + str + <?php echo date('Y') ?>  + "<?php echo date('m')?>"  + <?php echo $id -> id + 1; ?>);

Add quotes

Ragnar123
  • 5,174
  • 4
  • 24
  • 34
1

try passing your month as string instead of number

rahul
  • 7,573
  • 7
  • 39
  • 53
1

Store your content in a variable and do zero padding(zfill),take a look at this,

How to output integers with leading zeros in JavaScript

Community
  • 1
  • 1
Vivek S
  • 5,384
  • 8
  • 51
  • 72
  • Thanks, true that, integers no make sense with leading zeros. Great now I know about `zfill`. – watkib Sep 11 '12 at 07:17