auto generated purchase order number base from year in laravel - php

I have this code which should generate purchase number automatically with year attached to the number.
$record = Ponumbers::latest()->first();
$expNum = explode('-', $record->purchase_num);
//check first day in a year
if ( date('l',strtotime(date('Y-01-01'))) ){
$nextPoNumber = 'po'.date('Y').'-000001';
} else {
//increase 1 with last invoice number
$nextPoNumber = $expNum[0].'-'. $expNum[1]+1;
}
but it doesn't seems to work because it display only the this,
I am guessing it only displayed what's in this line
$nextPoNumber = 'po'.date('Y').'-000001';
any suggestion? thanks you so much in advance!

date('l',strtotime(date('Y-01-01'))) returns first weekday name for 2019, which is Tuesday. That is truthy value so the if is always true and $nextPoNumber is always po2019-000001.
If I understand your code correctly this is what you like it to do:
$record = Ponumbers::latest()->first();
$expNum = explode('-', $record->purchase_num);
$nextPoNumber = 'po'.date('Y').'-'.sprintf("%06d",$expNum[1]+1);

Try this :
$record = Ponumbers::latest()->first();
$expNum = explode('-', $record->purchase_num);
//check first day in a year
$first_day = gmdate('j', strtotime('first day of january this year'));
if (date('j') == $first_day){
$nextPoNumber = 'po'.date('Y').'-000001';
} else {
//increase 1 with last invoice number
$nextPoNumber = $expNum[0].'-'. $expNum[1]+1;
}

Related

Extract only the number of the day of the date and compare it with the current one

I need to extract only the day number of a user's registration date.
And extract only the day number of the current date.
Simply in an if loop, say if the day number the user registered is equal to the day number of the current date, do this, or do that.
Code:
$manager = "Manager";
$managerPRO = "ManagerPRO";
$q = $connessione->prepare("
SELECT * FROM collaboratori
WHERE cat_professionisti = ?
OR cat_professionisti = ?
");
$q->bind_param('ss', $manager,$managerPRO);
$q->execute();
$r = $q->get_result();
while($rr = mysqli_fetch_assoc($r)){
/*REGISTRATION DATE*/
$registrazione = $rr['data_registrazione'];
$timestamp = strtotime($registrazione);
echo date("d", $timestamp) .'=' ;
/*CURRENT DATE*/
$data_corrente = date('Y-m-d');
$timestamp_uno = strtotime($data_corrente);
echo date("d", $timestamp_uno);
/*CONTROL*/
if ($timestamp == $timestamp_uno){
echo "yes".'<br>';
}else{
echo "no".'<br>';
}
}
Result:
18=18no
17=18no
16=18no
16=18no
Why in the first case if 18 = 18 gives me false?
However, if I change the date of the user's registration and therefore the first 18, from 2020/11/18 to 2020/12/18, then the current month gives me yes!
I need that regardless of the month, just by checking the day if it is the same, tell me yes, where am I wrong?
You are comparing timestamps, which are measured in seconds. What you are doing is effectively comparing two different points in time, not the days of the month.
You really should be using DateTime. If you want to compare only the day part then you can do something like this.
$dt1 = new DateTime($registrazione);
$dt2 = new DateTime(); // defaults to now
if($dt1->format('d') === $dt2->format('d')) {
echo "Yes, it's the same day of the month";
} else {
echo 'no!';
}

Add data if current day is equal to last day of the month ONCE every month

I want to update my data if the current date is equal to last day of the month leave variable will update +1 once every month only, but it seems that when I refresh the homepage it keeps on adding.
function get_leave() {
$current_date = date('d'); // current date
$lastDayOfMonth = date('t'); // last day of the month
$leave = 1;
$total_add_leave = 0;
$query = $this->db->query('SELECT total_leave,user_id FROM user');
foreach ($query->result_array() as $row) {
if($current_date == $lastDayOfMonth) { //if current day is equal to last day of the month ADD 1
$total_add_leave = $row['total_leave'] + $leave;
}
$this->db->set('total_leave',$total_add_leave);
$this->db->where('user_id', $row['user_id']);
$this->db->update('user');
}
}
You can do this as below:
$current_date = date("y-m-d");
$last_day_of_the_month = date("y-m-t", strtotime($current_date));
if($current_date == $last_day_of_the_month) {
$total_add_leave = $row['total_leave'] + 1;
// database updating query must be placed in this condition
}
With this function you will find last date of month "t gives you last date of month"
date("Y-m-t");
then compare with current date
if(date("Y-m-d") == date("Y-m-t"))
{
// write your logic here
}

New count when month starts/ ends

I want to make a counter in php using only year, month and numbers. For example
Today's date is: 201710--- Where last 3 dash are variable. It could be any number in between 1-999.( I will add an auto increment in last 3 dash line ).
After end of October the dash line should start again from 1 for example
Example : 201711001 ....201711002....201711901.....201712001...201712010 etc
How can I do it ? Any Help ?
<?php
$patid = date("Ym").$patient_count;
?>
If you can keep track of the last used date and (obviously) $patient_count:
<php
//retrieve $lastdate and $patientcount (from database or file)
$newdate = date ("Ym");
if ($newdate != $lastdate) {
$patient_count++;
}
else {
$patient_count = 1;
}
$patid = $newdate.$patient_count;
$lastdate = $newdate;
//store $lastdate and $patientcount

Pick a value from an array randomly for a particular day

I have an array which has the id's and my code is as following,
//Random Offer of the day
$offers = Offer::model()->findAll();
$randomOffer = null;
$dataOffer = new GreenPointsActionDataObj();
if ($offers) {
$randomNo = mt_rand(0, count($offers) - 1);
$randomOffer = $offers[$randomNo];
$dataOffer->id = intval($randomOffer->id);
$dataOffer->title = $randomOffer->name;
$dataOffer->thumbUrl = $randomOffer->thumbUrl;
$dataOffer->description = $randomOffer->description;
}
$data[] = $dataOffer;
I need this to be day based, for example it should be always one for the day and next day another random but should be same the whole day.
how can i get this done ?
This is my suggestion, when an id is taken, i should maintain it with a day stored in DB.
Try this code:
Method 1:
To get the unique offer for year.
$weekday = date('l', time()); // will return the weekday number
$randomOffer = $offers[$weekday];
Method 2
To get the unique offer for year.
$daycnt = date('z', time())+1;
$randomOffer = $offers[$daycnt];
Method 3
To get the unique offer for month.
$cur_date = date('d', time());
$randomOffer = $offers[$cur_date ];

PHP. How to get a bimonthly recurring event?

I'm trying many approaches but then I get stuck half way.
Let's say order was created today. I need to display when the next recurring order will happen. So I have order created June 13, 2012. Then I have set the schedule to bimonthly recurring order, every 1st of month. How to calculate when the next recurring order will happen? The answer is August 1st.
If someone can outline an approach it would be very useful, it doesn't have to be code. This is what I have so far...
// first, get starting date
$start_date_month = date('m', strtotime($start_date));
// get this year
$this_year = date('Y');
// if this month is december, next month is january
$this_month = date('m', $timestamp_month);
if($this_month == 12){
$next_month = 1;
// if this month is not december add 1 to get next month
}else{
$next_month = $this_month + 1;
}
// get array of months where recurring orders will happen
$months = array();
for ($i=1; $i<=6; $i++) {
$add_month = $start_date_month+(2*$i); // 2, 4, 6, 8, 10, 12
if($add_month == 13){$add_month = 1;$year = $this_year+1;}
elseif($add_month == 14){$add_month = 2;$year = $this_year+1;}
elseif($add_month == 15){$add_month = 3;$year = $this_year+1;}
elseif($add_month == 16){$add_month = 4;$year = $this_year+1;}
elseif($add_month == 17){$add_month = 5;$year = $this_year+1;}
elseif($add_month == 18){$add_month = 6;$year = $this_year+1;}
elseif($add_month == 19){$add_month = 7;$year = $this_year+1;}
elseif($add_month == 20){$add_month = 8;$year = $this_year+1;}
else{$year = $this_year;}
echo $what_day.'-'.$add_month.'-'.$year.'<br />';
$months[] = $add_month;
}
echo '<pre>';
print_r($months);
echo '</pre>';
I don't want to simply find what's the date in two months from now. Let's say order created June 1. Next recurring order is August 1. Then let's say now, today is September 1st, but next recurring order is October 1st. See my dilemma?
Just take the current month, so since it's June, we get 6. 6 mod 2 == 0. Next month is July, we get 7. 7 mod 2 == 1.
So just check if current month % 2 == (first month % 2).
Then just check if it's the 1st of the month.
In PHP modulus is defined with the percentage symbol.
$month = date('n');
$createdMonth = 6;
if($month % 2 == $createdMonth % 2){
// stuff
}
You might find the library called When useful for this (I'm the author).
Here is code which will get you the next 2 recurring monthly dates (from todays date):
include 'When.php';
$r = new When();
$r->recur(new DateTime(), 'monthly')
->count(2)
->interval(2) // every other month
->bymonthday(array(1));
while($result = $r->next())
{
echo $result->format('c') . '<br />';
}
// output
// 2012-08-01T13:33:33-04:00
// 2012-10-01T13:33:33-04:00
Taking this a step further, you likely only want to find the 2 first business days:
include 'When.php';
$r = new When();
$r->recur(new DateTime(), 'monthly')
->count(2)
->interval(2) // every other month
->byday(array('MO', 'TU', 'WE', 'TH', 'FR')) // week days only
->bymonthday(array(1, 2, 3)) // the first weekday will fall on one of these days
->bysetpos(array(1)); // only return one per month
while($result = $r->next())
{
echo $result->format('c') . '<br />';
}
// output
// 2012-08-01T13:33:33-04:00
// 2012-10-01T13:33:33-04:00
Also note, the code is currently under a rewrite -- it works well but it is a little confusing and not well documented.
strtotime to the rescue:
<?php
date_default_timezone_set('Europe/London');
$d = new DateTime('2012-01-31');
$d->modify('first day of +2 months');
echo $d->format('r'), "\n";
?>
Let's say you want the next six orders:
$order_date = '6/13/2012';
$start = date('Y-m-01', strtotime($order_date));
$order_count = 6;
$future_orders = array();
$next = strtotime('+2 months', strtotime($start));
while(count($future_orders) < $order_count){
$future_orders[] = date('m/d/Y',$next);
$next = strtotime('+2 months', $next);
}
This can, obviously, be improved upon, but it should get you started ...
I got this:
$today = new DateTime();
$target_date = $today->modify("first day of +2 months");
echo "Your event is on " . $target_date->format("d/m/Y") . "!";

Categories