Search code examples
phpmysqldatabaseautonumber

How to get auto number based on selected date


please help me to get auto number based on selected date? i have working code for auto number but that's only for today/current date. what i want is if date options selected then auto number generated based on the date selected...

here is my sample code for current date

    $today=date("ymd");
    $query = "SELECT max(id) AS last FROM member WHERE id LIKE '$today%'";
    $hasil = mysql_query($query);
    $data  = mysql_fetch_array($hasil);
    $lastNoTransaksi = $data['last'];
    $lastNoUrut = substr($lastNoTransaksi, 8, 2);
    $nextNoUrut = $lastNoUrut + 1;
    $nextNoTransaksi = $today.sprintf('.%02s', $nextNoUrut);?>

this is my full code with html selected date

<?php
require("db.php");
$today=date("ymd");
$query = "SELECT max(id) AS last FROM member WHERE id LIKE '$today%'";
$hasil = mysql_query($query);
$data  = mysql_fetch_array($hasil);
$lastNoTransaksi = $data['last'];
$lastNoUrut = substr($lastNoTransaksi, 8, 2);
$nextNoUrut = $lastNoUrut + 1;
$nextNoTransaksi = $today.sprintf('.%02s', $nextNoUrut);?>



<form method="post">
<table align="center">
<tr ><td>Date</td>
<td><input type="date" name="date" /></td>
</tr>
<tr><td>Code</td>
<td>
<?php echo $nextNoTransaksi; ?></td>
</tr>
<tr >
<td>Name</td>
<td><input type="text" name="name" /></td>
</tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Input" /></td>
</tr>
</table>

thanks so much for you kindly help


Solution

  • You can convert a date to unix timestamp and use that to generate your number.

    $date = date('ymd', strtotime('2014-03-20'));
    

    In your case

    $date = date('ymd', strtotime($_POST['date']));
    

    You can also use time() to get the current unix timestamp and modify it

    time() + 60*60; // current time + 1 hour