Connecting Tech Pros Worldwide Forums | Help | Site Map

Parse the date into integer

Newbie
 
Join Date: Sep 2007
Location: indonesia
Posts: 25
#1: Sep 24 '07
I' making an database application. In one form (Input data Form), I need to fill the text field with the reference number automatically. This number must be start with date of today. For the example, 2007092401. And the text field must be filled with the largest number in database (the largest of that day).
Firstly, the form will check if there's a number start with today's date. If it's not NULL, the form takes the biggest one. But if it returns NULL, I wanna make a number that start with today's date. But I don't know how to join the date (I take the date with time's function) and the '01'...
(PS : the type of my reference number's field in MySQL is integer)

Here's a piece of my code.

Expand|Select|Wrap|Line Numbers
  1. $now=time();
  2. $ref=date("Ymd",$now);
  3.  
  4. $refku=implode($ref,"01");  // I need some help here
  5.  
  6. $peri="select max(noreff) as A from tblref where noreff like '$ref%' ";
  7. $hasil=mysql_query($peri);
  8. $row=mysql_fetch_array($hasil);
  9.  
  10. if($row!=NULL)
  11. {
  12. echo (" <input name=\"noreff\" type=\"text\" value=\"$row[A]\" maxlength=10></td> ");
  13. }
  14.  
  15. else
  16. {
  17. echo (" <input name=\"noreff\" type=\"text\" value=\"$refku\" maxlength=10 /></td> ");
  18. }    
  19.  
---Thank You---

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#2: Sep 24 '07

re: Parse the date into integer


Hi momogi.

You could simply do this:
Expand|Select|Wrap|Line Numbers
  1. $refku= $ref ."01";
  2.  
$refku would technically be a string, but PHP is smart enough to convert it into whatever type it needs to be in various situations.

For example:
Expand|Select|Wrap|Line Numbers
  1. $refku = date("Ymd", time()) . "01";
  2. # Now $refku is a string "2007092401"
  3.  
  4. echo "refku plus one: ". ($refku + 1);
  5. # Will output "refku plus one: 2007092402"
  6.  
Newbie
 
Join Date: Sep 2007
Location: indonesia
Posts: 25
#3: Sep 24 '07

re: Parse the date into integer


Quote:

Originally Posted by Atli

Hi momogi.

You could simply do this:

Expand|Select|Wrap|Line Numbers
  1. $refku= $ref ."01";
  2.  
$refku would technically be a string, but PHP is smart enough to convert it into whatever type it needs to be in various situations.

For example:
Expand|Select|Wrap|Line Numbers
  1. $refku = date("Ymd", time()) . "01";
  2. # Now $refku is a string "2007092401"
  3.  
  4. echo "refku plus one: ". ($refku + 1);
  5. # Will output "refku plus one: 2007092402"
  6.  

Hi Atli...

thx 4 your reply... :)

But I have to say that it doesn't work.
My text field still empty,,, (except I enter the today's reference manually in database).

Is there another solution? thx b4.
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#4: Sep 24 '07

re: Parse the date into integer


Quote:
But I have to say that it doesn't work.Is there another solution?
Atli's solution will work.
You have obviously asked the wrong question which was
Quote:
Parse the date into integer
Quote:
My text field still empty,,,
Is this you problem? If so, what text field and where?
Newbie
 
Join Date: Sep 2007
Location: indonesia
Posts: 25
#5: Sep 25 '07

re: Parse the date into integer


Quote:

Originally Posted by code green

Atli's solution will work.
You have obviously asked the wrong question which was Is this you problem? If so, what text field and where?

Hi...

The text field is a field where I wanna put my reference number automatically. its placed in the top of my form.
if the user haven't fill the form at the day, the form will generate the reference number automatically which started with the date.

hope that was clear 4 you. I'm sorry if I was ask the wrong question...

gbu.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#6: Sep 25 '07

re: Parse the date into integer


Try something like this:
Expand|Select|Wrap|Line Numbers
  1. # Execute query and get row
  2. $ref = date("Ymd", time());
  3. $query = "SELECT MAX(noreff) AS 'A' FROM tblref WHERE noreff LIKE '$ref%' ";
  4. $result=mysql_query($query) or die("Query failed");
  5. $row=mysql_fetch_assoc($result);
  6.  
  7. # Create reference
  8. if(@isset($row['A'])) {
  9.   $refku = $row['A'];
  10. }
  11. else {
  12.   $refku = $ref . "01";
  13. }
  14.  
  15. # Print reference
  16. echo "<input name=\"noreff\" type=\"text\" value=\"$refku\" maxlength=10 /></td> "
  17.  
Newbie
 
Join Date: Sep 2007
Location: indonesia
Posts: 25
#7: Sep 25 '07

re: Parse the date into integer


Quote:

Originally Posted by Atli

Try something like this:

Expand|Select|Wrap|Line Numbers
  1. # Execute query and get row
  2. $ref = date("Ymd", time());
  3. $query = "SELECT MAX(noreff) AS 'A' FROM tblref WHERE noreff LIKE '$ref%' ";
  4. $result=mysql_query($query) or die("Query failed");
  5. $row=mysql_fetch_assoc($result);
  6.  
  7. # Create reference
  8. if(@isset($row['A'])) {
  9.   $refku = $row['A'];
  10. }
  11. else {
  12.   $refku = $ref . "01";
  13. }
  14.  
  15. # Print reference
  16. echo ("<input name=\"noreff\" type=\"text\" value=\"$refku\" maxlength=10 /></td> "
  17. }    
  18.  

whoaaa... it works!!!

thanks...

thanks... :)

and thanks... :D
Reply