I am generating reference no REF-082013-001 (REF-mmyyyy-001) and incrementing the last part for every entry. REF-082013-001 for first record and REF-082013-002 for second and so on.
My Question is How can i reset the last number by php for every new month. Let say September 2013 I want it to be REF-09-2013-001 and auto increment it till the end of September and then in November reset it again.
Is there a ways to do this in php. Your help is much appreciated. Thank you
Update
Currently 'REF-'.date('m-Y').$maxnumberfromdb in single column called reference_no and Now thinking to store the mm-yyyy ref1 and last number in ref2 separately and start ref2 every first day of the month.
You can probably go with a single table, with an AUTO_INCREMENT field to handle the last part of the reference number, and a date/time field to track when it was last reset.
CREATE TABLE track_reference
ref_number INT(11) AUTO_INCREMENT,
last_reset DATETIME;
Then you write a function in PHP to get a new reference number, which (pseudo-code):
if (MONTH(time()) > MONTH(last_reset)) {
reset ref_number to 0 (use ALTER TABLE);
}
select (ref_number) into $variable;
return 'REF_<month><year>_$variable;
}
It's rough, but you get the idea. I'd also have YEAR appear before MONTH for making sorting by reference number easier later.
This is my code for reset counter and update last_reset, this is effective for me
<pre>
$date2 = new DateTime("now", new DateTimeZone('America/New_York') );
$month_end = strtotime('last day of this month', time());
$count = $data['sim_tarif_count'];
$date3=$date2->format('Y-m-d');
foreach ( $count as $r2 ){
$counts[] = $r2['count'];
$last_reset[] = $r2['last_reset'];}
$paddedNum = sprintf("%04d", $counts[0]);
$reg_id = 'SEQ'.$date2->format('Ymd').$paddedNum;
echo " ";
echo date('j', $month_end);
$counter = $counts[0]+1;
//for reset
if($date2->format('Y-m') > $last_reset[0])
{
$counter = 0;
$counting_update=$this->docs_model->update_time_counter($counter,$date3);
echo $counter = 0;
} else
{
$counting_update=$this->docs_model->update_time_count($counter);
}
</pre>
Related
I have a date that goes into a loop that the user specifies. The date will always come from the database formatted as a 'Y-m-d' string. I am aware that I can compare the strings directly as long as they are in that format, however, I have also tried using strtotime to convert the dates to compare them with no luck. I am trying to determine how many paycheck a user has before a payment is due
Here is what I have
$due_date = '2016-12-13';
//count paychecks set to zero and added to by loop
$paychecks = 0;
//users next paycheck ('Y-m-d' ALWAYS)
$next_payday = $user['next_payday']; //equal to '2016-12-02'
//how often they get paid (int)
$frequency = 14;
while(strtotime($next_payday) <= strtotime($due_date)){
//next_payday equals 1480654800 when coming into the loop
//due_date equals 1481605200 when coming into the loop
//add 14 days to the date
$next_payday = date('Y-m-d', strtotime("+" .$frequency." days"));;
//add to paychecks
$paychecks++;
}
The problem is that the loop never stops. It keeps going and going.
Thanks for any help anyone can give me.
Ah, be sure to use strtotime to get integers (representing number of seconds since the epoch) for comparison and multiply your frequency of days by the number of seconds in a day (86400):
$due_date = strtotime('2016-12-25');
//count paychecks set to zero and added to by loop
$paychecks = 0;
//users next paycheck (unixtime for comparison)
$next_payday = strtotime($user['next_payday']);
//how often they get paid (int)
$frequency = 14;
while($next_payday <= $due_date){
//add 14 days to the date
$next_payday += ($frequency * 86400);
//add to paychecks
$paychecks++;
}
I am currently attempting to get a list of dates from a current date using the following format so that I can process it and stick it in my database
Saturday/02-05-2015
So far, i've managed to get the system to output the date correctly, but can not get it to increment in single day values.
My current code to attempt to increment this is the following
$tempStartDateN = ("$splode[0]/$splode[1]/$splode[2]/$splode[3]");
echo $tempStartDateN;
$tempStartDateN = date('l/d/m/Y', strtotime($tempStartDateN . ' + 1 day'));
echo $tempStartDateN;
I am currently using explode to process the data after the increment, which works fine, but can not get the date itself to increment as long as the day name is included.
Currently, the time is got using this code, which is processed afterwords using explode
$OldDateArray = date("Y/m/d/l");
So to keep a long question short, what is the best way to increment a date that requires the day name, day, month then year?
EDIT:
Heres my current code, managed to get this far thanks to SamV
$date = date("l/d/m/Y");
echo $date;
echo ('</br>');
list($weekdayName, $dateString) = explode("/", $date, 2);
$dateObj = new \DateTime($dateString);
for($i=0; $i<=5; $i++){
$dateObj->add(new \DateInterval("P1D")); // P1D stands for "Period 1 Day"
echo $dateObj->format("l/d/m/Y"); // Sunday/03/05/2015
echo ('</br>');
}
What this does however is:
Friday/01/05/2015
Tuesday/06/01/2015
Wednesday/07/01/2015
Thursday/08/01/2015
Friday/09/01/2015
Saturday/10/01/2015
Sunday/11/01/2015
this means that date and month are swapping around, what is causing this?
You don't need to parse the week day name to add days onto a date.
$date = "Saturday/02-05-2015";
list($weekdayName, $dateString) = explode("/", $date, 2); // Parse "02-05-2015"
$dateObj = new \DateTime($dateString);
$dateObj->add(new \DateInterval("P1D")); // P1D stands for "Period 1 Day"
echo $dateObj->format("l/d/m/Y"); // Sunday/03/05/2015
I used the DateTime class, here is the documentation.
I wrote out what you are trying to do yourself, not sure what is causing your issue. This code works though.
$date = "Friday/01-05-2015";
list($weekdayName, $dateString) = explode("/", $date, 2); // Parse "01-05-2015"
$dateObj = new \DateTime($dateString);
for($i = 0; $i < 5; $i++) {
$dateObj->add(new \DateInterval("P1D")); // P1D stands for "Period 1 Day"
echo $dateObj->format("l/d/m/Y") . '<br>';
}
Outputs:
Saturday/02/05/2015
Sunday/03/05/2015
Monday/04/05/2015
Tuesday/05/05/2015
Wednesday/06/05/2015
If strtotime is able to parse a date it returns the timestamp. Why not add to it the number of seconds in a day? Smth. like $timestamp += 24 * 3600;
P.S. As far as I can understand, strtotime may accept timestamp as second argument (http://us2.php.net/manual/en/function.strtotime.php) smth. like $timestamp = strtotime('+1 day', $timestamp);
I am building a php application using pgsql as its back end.
I would like to increment the date by some amount of date shich should be loaded from my database which have given value as available=1,3,5(implying monday,wednesday,friday of a week).I would like to increment these available values to current date. I am using N format in date() function to represent the values of days in a week as 1 to 7 which is stored in available field in the database
If current date =22-07-2013 which is monday,then i have to increment this to wednesday(available=3) and then to friday(available=5) And then to monday of the next week.
And so on..
but i cant do that..
i am in need of such a code where the value of available may change according to the tuples in that tuple.So i would like to increment the current date based on the value of available.
So please help me to achieve it.
The code I used is attached herewith.Please have a look at it.
<?php
$sq = "SELECT * FROM $db->db_schema.dept where active='Y' and dept_id=$dept_id";
$result = $db->query($sq);
$ftime=$result[0]['f_time'];
$ttime=$result[0]['t_time'];
$a=date('Y-m-d').$ftime;
$b=date('Y-m-d').$ttime;
$to_time = strtotime("$b");
$from_time = strtotime("$a");
$minutes= round(abs($to_time - $from_time) / 60,2). " minute";
$days=array();
$days= explode("," , $result[0]['available']);
$result[0]['available'];
$intl=$result[0]['slot_interval'];
$slots=$minutes/$intl;
$dt1 =date("m/d/Y $ftime ");
$s_mnts=explode(":",$ftime);
$m= date('N');
-- $dt=array();
$a=$dt1;
$l=0;
for($n=1;$n<=3;$n++)
{
for($k=$m;$k<=7;$k++)
{ $l=$l+1;
if(in_array($m,$days))
{
echo "dasdsa";
echo date("Y-m-d H:i:s", strtotime("$a +$l days"));
echo"<br>";
}
$m=$m+1;
if($m==7){$m=1;}
}
}
?>
where
dept_id -> primary key of the table dept
$db->query($sq); -> query is used to fetch the given values and is defined in another file named database.php in the program folder.
f_time and t_time -> fields in the table dept which describes the from_time and to_time.f_time is the time from which we have to start increment and t_time is the time to end this increment.
Please inform me whether there is any improvement in the code I have given. .
What you could do is something like this:
You say how many days you want to increment. And give an array of availables.
<?php
$inicialDate = time(); //Timestamp of the date that you are based on
$tmpDate = $inicialDate; //Copy values for tmp var
$increment = 5; //Increment five days
$available = [1,3,5]; //Days available
/*Ok, now the logic*/
while($increment > 0){
$tmpDate = strtotime("+1 day", $tmpDate); //Increase tmpdate by one day
if(in_array(date("N",$tmpDate), $available){ //If this day is one of the availables
$increment--;
}
}
$finalDate = date("m/d/Y",$tmpDate);
?>
This logic should work, although I don't know how to reproduce it via a SQL Procedure.
From what I can tell, you are after something like
UPDATE sometable
SET some_date_column = some_date_column + ('1 day'::INTERVAL * some_integer_value);
I have the following code:
$ips = file_get_contents($_SERVER['DOCUMENT_ROOT']."/visitors.txt");
$arr = explode(",",$ips);
$today = strtotime(date('Y-m-d H:i:s'));
for ($n = 0, $max = count($arr); $n <= $max; $n++) {
$visArr = explode("#",$arr[$n]);
$visDate = strtotime($visArr[1]); //$visArr[1] = 2011-12-27 14:10:45
if($visDate < $today){
unset ($arr[$n]); //remove array item if its date not within 24 hours
}
}
The data is stored like this:
xxx.xxx.xxx.xxx#2011-12-27 11:56:24,
xxx.xxx.xxx.xxx#2011-12-28 11:56:24,
I want to get the visitors from the last 24 hours.
I don't want to use MySQL db, I just want to use the txt file but I'm stuck.
Thanks in advance.
I can see 2 problems:
1st you are comparing the stored time with current time and saying that it will filter array item if its date not within 24 hours..
I think you should use $today = strtotime("-1 day");
and name yesterday instead of today..
Secondly and reason of error is that you are exploding data in file with , which will give you "" ie null for last element in array..and thats why strtotime function is giving error for that value..
what you should do is:
if($visArr[1])
{
$visDate = strtotime($visArr[1]); //$visArr[1] = 2011-12-27 14:10:45
if($visDate < $today){
unset ($arr[$n]); //remove array item if its date not within 24 hours
}
}
I had this problem some years ago and back then I implemented a "different logic" in order to deliver the project but the doubt remains in my mind and hopefully with your help I'll be able to understand it now.
Suppose I have some scheduled events on my database that may or may not spawn over several days:
id event start end
-----------------------------------------------
1 fishing trip 2009-12-15 2009-12-15
2 fishCON 2009-12-18 2009-12-20
3 fishXMAS 2009-12-24 2009-12-25
Now I wish to display the events in a calendar, lets take the month of December:
for ($day = 1; $day <= 31; $day++)
{
if (dayHasEvents('2009-12-' . $day) === true)
{
// display the day number w/ a link
}
else
{
// display the day number
}
}
What query should the dayHasEvents() function do to check if there are (or not) events for the day? I'm guessing SELECT .. WHERE .. BETWEEN makes the most sense here but I've no idea how to implement it. Am I in the right direction?
Thanks in advance!
#James:
Lets say we're on December 19th:
SELECT *
FROM events
WHERE start >= '2009-12-19 00:00:00'
AND end <= '2009-12-19 23:59:59'
Should return the event #2, but returns nothing. =\
You should scratch that approach and grab all events for the given month up front so you only need to perform a single query as opposed to N queries where N is the number of days in the month.
You could then store the returned results in a multidimensional array like so:
// assume event results are in an array of objects in $result
$events = array();
foreach ($result as $r) {
// add event month and day as they key index
$key = (int) date('j', strtotime($r->start));
// store entire returned result in array referenced by key
$events[$key][] = $r;
}
Now you'll have a multidimensional array of events for the given month with the key being the day. You can easily check if any events exist on a given day by doing:
$day = 21;
if (!empty($events[$day])) {
// events found, iterate over all events
foreach ($events[$day] as $event) {
// output event result as an example
var_dump($event);
}
}
You're definitely on the right track. Here is how I would go about doing it:
SELECT *
FROM events
WHERE start <= '2009-12-01 00:00:00'
AND end >= '2009-12-01 23:59:59'
And you obviously just replace those date values with the day you're checking on.
James has the right idea on the SQL statement. You definitely don't want to run multiple MySQL SELECTs from within a for loop. If daysHasEvents runs a SELECT that's 31 separate SQL queries. Ouch! What a performance killer.
Instead, load the days of the month that have events into an array (using one SQL query) and then iterate through the days. Something like this:
$sql= "SELECT start, end FROM events WHERE start >= '2009-12-01' AND end <= '2009-12-31'";
$r= mysql_query($sql);
$dates= array();
while ($row = mysql_fetch_assoc($r)) {
// process the entry into a lookup
$start= date('Y-m-d', strtotime($row['start']));
if (!isset($dates[$start])) $dates[$start]= array();
$dates[$start][]= $row;
$end= date('Y-m-d', strtotime($row['end']));
if ($end != $start) {
if (!isset($dates[$end])) $dates[$end]= array();
$dates[$end][]= $row;
}
}
// Then step through the days of the month and check for entries for each day:
for ($day = 1; $day <= 31; $day++)
{
$d= sprintf('2009-12-%02d', $day);
if (isset($dates[$d])) {
// display the day number w/ a link
} else {
// display the day number
}
}
For your purposes a better SQL statement would be one that grabs the start date and the number of events on each day. This statement will only work properly if the start column is date column with no time component:
$sql= "SELECT start, end, COUNT(*) events_count FROM events WHERE start >= '2009-12-01' AND end <= '2009-12-31' GROUP BY start, end";