find last weekday in php4.0 - php

i was trying to find last weekday using code
$mnt = "Apr 2013"
echo date('d-M-Y', strtotime('last week day', strtotime($mnt)));
but it is displaying 11-Apr-2013
please help me to find what went wrong.
Thanks in advance.

This function will probably work on PHP >= 4.x versions:
function lastWeekDay($mnt) {
$result = strtotime("1 $mnt");
$result = mktime(0, 0, 0, date('n', $result) + 1, 0, date('Y', $result));
while (in_array(date('D', $result), array('Sat', 'Sun'))) $result -= 86400;
return $result;
}
echo date('d-M-Y', lastWeekDay('Apr 2013')); # 30-Apr-2013
demo

<?php
function lastWeekDay ($mnt)
{
$result = strtotime("last day of $mnt");
$day = date('D', $result);
if ('Sun' === $day)
{
$result -= 86400 * 2;
}
if ('Sat' === $day)
{
$result -= 86400;
}
return $result;
}
echo date('d-M-Y', lastWeekDay('Apr 2013'));

Related

installment due by period - PHP

the code snippet below shows a simulation of installments, with fixed expiration dates, divided every 30 days!
I would like to include a periodic expiration date, for example, every 20 days, or 10 every 10, depending on the variable $periodicity;
I have no idea how to do it?
<?php
function calculate_due($num_installment, $first_due_date = null){
if($first_due_date != null)
{
$first_due_date = explode('/',$first_due_date);
$day = $first_due_date[0];
$month = $first_due_date[1];
$year = $first_due_date[2];
}
else
{
$day = date('d');
$month = date('m');
$year = date('Y');
}
$periodicity = 20;
for($installment = 0; $installment < $num_installment; $installment++)
{
if ($periodicity == 30)
echo date('d/m/Y', strtotime('+'.$installment. " month", mktime(0, 0, 0, $month, $day, $year))),'<br/>';
else
echo date('d/m/Y', strtotime('+'.$installment. " month", mktime(0, 0, 0, $month, $periodicity, $year))),'<br/>';
}
}
echo 'Calculates installments from an informed date<br/>';
calculate_due(5, '10/10/2020');
Try this,
<?php
function calculate_due($num_installment, $first_due_date = null, $days = 1){
$start = DateTime::createFromFormat('d/m/Y', $first_due_date);
$end = DateTime::createFromFormat('d/m/Y', $first_due_date);
$end->add(new DateInterval('P'.($num_installment * $days).'D'));
$period = new DatePeriod(
$start,
new DateInterval('P'.$days.'D'),
$end
);
$return = [];
foreach ($period as $date) {
$return[] = $date->format('d/m/Y');
}
return $return;
}
echo 'Calculates installments from an informed date<br/>'.PHP_EOL;
echo implode("\n", calculate_due(5, '10/10/2020', 20));
https://3v4l.org/TLR8a
Change 20 (the last function parameter) to suit the number of days between.
Result:
Calculates installments from an informed date<br/>
10/10/2020<br/>
30/10/2020<br/>
19/11/2020<br/>
09/12/2020<br/>
29/12/2020<br/>

Transform PHP script to a general-case function

I have this code that modify date in the way I want, for example, if starting date is 31/01/2000, adding 1 month will return 29/02/2000, then, 31/03/2000.
If date is 30/01/2000 (not last day of the month) it will return 29/02/2000, then 30/03/2000, 30/04/2000 and so on.
I want to transfirm that code in a general case function, to be able to add 1,3,6,12 months, and inside the for loop, to work all corect.
I would like it to be a function 2 or 3 arguments, startingDate, duration(nr of iterations), frequency (add 1/3/6/12 months per once).
<?php
$date = new DateTime('2000-01-28'); // or whatever
#echo $date->format('d')." ".$date->format('t');
$expectedDay = $date->format('d');
$month = $date->format('m');
$year = $date->format('Y');
for ($i = 0; $i < 100; $i++) {
echo $date->format('Y-m-d') . "<br>";
if ($month++ == 12) {
$year++;
$month = 1;
}
$date->modify("${year}-${month}-1");
if ($expectedDay > $date->format('t')) {
$day = $date->format('t');
} else {
$day = $expectedDay;
}
$date->modify("${year}-${month}-${day}");
}
Whelp, there's an extremely easy function for this in PHP nowadays.
first, you get a timestamp instead of a datetime:
$timestamp = $date->getTimestamp();
Now, just use strtotime to add onto the date.
strtotime("+1 month", $myTimestamp);
You can change the +1 into anything you want, so you just throw the amount in the string and voila; a dynamic way of adding them!
however, since you want to do +30 days instead of a natural month, you're better off just adding 30 days to the timestamp, like so:
$timestamp = $timestamp + (3600 * 24 * 30); //s per h * h per d * d
So, you'd end up with something like this:
function calculateTime($startingDate, $iterations, $frequency){
$timeStamp = strtotime($startingDate);//if you expect a string date
$timeToAdd = (3600 * 24 * 30) * $frequency; //30 days * frequency
$return = array();
$return[] = date('Y-m-d', $timeStamp); //Original date
$previousDate = $timeStamp; //Original date for now
for($i = 0; $i < $iterations; $i++){
$newDate = $previousDate + (3600 * 24 * 30);
$return[] = date('Y-m-d', $newDate);
$previousDate = $newDate;
}
return $return;
}
And then for the rendering part:
//Let's render this stuff
$dates = calculateTime('24-08-2017', 25, 3);
foreach($dates as $date){
echo "$date</br>";
}
If you'd like to do it with full months, something like this:
<?php
function calculateTime($startingDate, $iterations, $frequency){
$timeStamp = strtotime($startingDate);//if you expect a string date
$return = array();
$return[] = date('Y-m-d', $timeStamp); //Original date
$previousDate = $timeStamp; //Original date for now
for($i = 0; $i < $iterations; $i++){
$lastDay = false;
//It's the last day of the month
if(date('t', $timeStamp) == date('d', $timeStamp)){
$lastDay = true;
}
if($frequency == 12){
$newDate = strtotime('+1 year', $previousDate);
}
else{
if($lastDay){
$firstDayOfMonth = strtotime(date("01-m-Y", $previousDate));
$newDate =strtotime("+$frequency month", $firstDayOfMonth);
}
else{
$newDate = strtotime("+$frequency month", $previousDate);
}
}
if($lastDay){
$return[] = date('Y-m-t', $newDate);
}
else{
$return[] = date('Y-m-d', $newDate);
}
$previousDate = $newDate;
}
return $return;
}
//Let's render this stuff
$dates = calculateTime('31-01-2000', 25, 1);
foreach($dates as $date){
echo "$date</br>";
}
I hope this helps? :)
If you'd like to see how this works quickly, just paste my code into a phpfiddle. Unfortunately the save function is broken right now.

Display Range of Date in Specific Month

I want to display date range for specific month in php as shown below:
Suppose i select month January and year 2015 then code should display :
Date 01/01/2015 to 10/01/2015
10/01/2015 to 20/01/2015
21/01/2015 to 31/01/2015
Kindly help me out to resolve this issue as soon as possible.
thanks in advance..
try this code
$list=array();
$month = 11;
$year = 2014;
for($d=1; $d<=31; $d++)
{
$time=mktime(12, 0, 0, $month, $d, $year);
if (date('m', $time)==$month)
$list[]=date('Y-m-d-D', $time);
}
echo "<pre>";
print_r($list);
echo "</br>";
echo $list[0] . " to " . $list[9] ."</br>";
echo $list[10]. " to " .$list[19] ."</br>";
echo $list[20]. " to " . end($list) ."</br>";
echo "</pre>";
output
2014-11-01-Sat to 2014-11-10-Mon
2014-11-11-Tue to 2014-11-20-Thu
2014-11-21-Fri to 2014-11-30-Sun
try this
function dateRange( $first, $last, $step = '+10 days', $format = 'Y/m/d' ) {
$dates = array();
$current = strtotime( $first );
$last = strtotime( $last );
while( $current <= $last ) {
$dates[] = date( $format, $current );
$current = strtotime( $step, $current );
}
return $dates;
}
$dates = dateRange( '2010/07/26', '2010/08/05') ;
foreach($dates as $date)
{
echo $date;
}

PHP - Loop while 'false', stop when it is 'true'

I want to get tomorrow date. But if tomorrow is Saturday or Sunday. It will get date in Monday. It will skip Saturday and Sunday. Also if tomorrow is holiday, it will skip the date and get the next date. I have list of holiday date in my database.
I've trying this code. Suppose today is Friday, May 24.
$today = "2013-05-24";
$tommorow = date('Y-m-d', strtotime($today . ' + 1 day'));
$valid = check_valid($tommorow);
while (!$valid){
$tommorow = date('Y-m-d', strtotime($today . ' + 1 day'));
$valid = check_valid($tommorow);
if($valid){
break;
}
}
function check_valid($date){
return true;
$timestamp = strtotime($date);
$day = date('D', $timestamp);
if ($day == "Sat" || $day == "Sun"){
return false;
}
$mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$date'");
if (mysql_num_rows($mysql) >= 1){
return false;
}
}
I'm using while clause because i assume that there is holiday date that appears in two or three days in a row. For example, holiday is on May 27 and 28, then tomorrow date should be in Wednesday.
Any ideas?
Could you help me please?
If you have another approach to achieve this, I also want to know it.
Thank you,
Here is the code, doing, what you need:
It uses DateTime class and do-while loop:
<?php
function not_valid($date){
$weekday = $date->format('l');
if ($weekday == "Saturday" || $weekday == "Sunday"){
return true;
}
$str_date = $date->format('Y-m-d');
$mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$str_date'");
if (mysql_num_rows($mysql) >= 1){
return true;
}
return false;
}
$today = new DateTime("2013-05-24");
do{
//add 1 day
$today->add(new DateInterval('P1D'));
}while(not_valid($today));
print $today->format('Y-m-d');
?>
At the beginning it is 24.05.2013 - Friday. Then it adds 1 to it. 25 and 26 are Saturday and Sunday, so it skips them and prints 2013-05-27.
As Syjin have already said, your check_valid() will always return true because you've got an return true; at the beginning of your function.
Move it to the end of your function to return true if the function hasn't returned false before.
This function checks (with your logic) if a timestamp is valid and if not searches for the next valid timestamp. it will return a timestamp of the first valid timestamp possible.
function getNextValidDay($timestamp) {
$day = date('D', $timestamp);
if ($day == "Sat" || $day == "Sun"){
$timestamp += 60*60*24;
return getNextValidDay($timestamp);
}
$mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$timestamp'");
if (mysql_num_rows($mysql) >= 1){
$timestamp += 60*60*24;
return getNextValidDay($timestamp);
}
return $timestamp;
}
The problem is in the function check_valid(). You have returned true in the very beginning of the function so it will not allow to process remaining code so it should be something like this,
function check_valid($date){
$timestamp = strtotime($date);
$day = date('D', $timestamp);
if ($day == "Sat" || $day == "Sun"){
return false;
}
$mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$date'");
if (mysql_num_rows($mysql) >= 1){
return false;
}
return true;
}
you should update/increment '+ 1 day' in each loop. change your code to be something like this
$today = "2013-05-24";
$ii_day = 1;
$tommorow = date('Y-m-d', strtotime($today . ' + '.($ii_day).' day'));
$valid = check_valid($tommorow);
while (!$valid){
$ii_day++;
$tommorow = date('Y-m-d', strtotime($today . ' + '.($ii_day).' day'));
$valid = check_valid($tommorow);
if($valid){
break;
}
}
and then on your check_valid function, change your code a bit.
function check_valid($date){
//return true;
$timestamp = strtotime($date);
$day = date('D', $timestamp);
if ($day == "Sat" || $day == "Sun"){
return false;
}
else {
$mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$date'");
if (mysql_num_rows($mysql) >= 1){
return false;
}
else
return true;
}
}
hopefully it works..:)
You can try this solution, it's based on my working days code that I use in a couple of my projects. It uses Swedish holidays but you can adjust these to the holidays that are observed in your country. Remember that many holidays depend on the current year.
function next_valid_day() {
$year = date('Y');
$easter_date = easter_date($year);
$holidays = array(
mktime(0, 0, 0, 1, 1, $year), // Nyårsdagen
mktime(0, 0, 0, 1, 6, $year), // Trettondedag jul
//strtotime('-3 days', $easter_date), // Skärtorsdagen
strtotime('-2 days', $easter_date), // Långfredagen
strtotime('-1 day', $easter_date), // Påskafton
$easter_date, // Påskdagen
strtotime('+1 day', $easter_date), // Annandag påsk
mktime(0, 0, 0, 5, 1, $year), // 1:a maj
strtotime('+39 days', $easter_date), // Kristi himmelsfärdsdag
mktime(0, 0, 0, 6, 6, $year), // Sveriges nationaldag
strtotime('-1 day', strtotime('next Saturday', mktime(0, 0, 0, 6, 19, $year))), // Midsommarafton
mktime(0, 0, 0, 12, 24, $year), // Julafton
mktime(0, 0, 0, 12, 25, $year), // Juldagen
mktime(0, 0, 0, 12, 26, $year), // Annandag jul
mktime(0, 0, 0, 12, 31, $year) // Nyårsafton
);
$valid_date = false;
$d = 0;
while ($valid_date === false) {
$date = strtotime('+' . ++$d . ' days');
if (!in_array($date, $holidays) && !(date('N', $date) > 5)) {
$valid_date = true;
}
}
return date('Y-m-d', $date);
}
var_dump(next_valid_day());

PHP to calculate latest 31 March

i want to calculate latest 31-Mar .... suppose date is 1-Jan-2012 i want result as 31-mar-2011 and if is 1-April-2011 then also i want result 31-mar-2011 and if its 1-mar-2011 it should come as 31-mar-2010.....hope i made my self clear ...(with php) ... i al calculating date with this for financial year ...
always between 31-mar-lastyear to 1-April-thisyear
... year should be taken automatically ...
i was trying like this
31-mar-date('y') and 31-mar-date('y')-1
but its not working as its taking current year every time.
Here is an example using the wonderful strtotime function of php.
<?php
$day = 1;
$month = 1;
$year = 2011;
$date = mktime(12, 0, 0, $month, $day, $year);
$targetMonth = 3;
$difference = $month - $targetMonth;
if($difference < 0) {
$difference += 12;
}
$sameDateInMarch = strtotime("- " . $difference . " months", $date);
echo "Entered date: " . date("d M Y", $date) . "<br />";
echo "Last 31 march: " . date("t M Y", $sameDateInMarch);
// the parameter 't' displays the last day of the month
?>
Something like this:
function getLast() {
$currentYear = date('Y');
// Check if it happened this year, AND it's not in the future.
$today = new DateTime();
if (checkdate(3, 31, $currentYear) && $today->getTimestamp() > mktime(0, 0, 0, 3, 31, $currentYear)) {
return $currentYear;
}
while (--$currentYear) {
if (checkdate(3, 31, $currentYear)) {
return $currentYear;
}
}
return false;
}
var_dump(getLast());
It should return the year or false.
if (date('m')>3) {
$year = date('Y').'-'.(date('Y')+1);
} else {
$year = (date('Y')-1).'-'.date('Y');
}
echo $year;
this is to get the current financial year for India

Categories