PHP What is wrong with recursive function? - php

i did function for showing today date or next workday date, if is weekend. Function works great, but with return is something wrong.
$today = todayDate('2014-10-18'); // Saturday
function todayDate($date) {
if(date('N', strtotime($date)) >= 6) {
echo 'If - ' . $date . '<br/>';
$date = date('Y-m-d', strtotime('+1 day', strtotime($date)));
todayDate($date);
} else {
echo 'Else - ' . $date . '<br/>';
}
return $date;
}
echo '<br/><br/>Today: ' . $today . '<br/><br/>';
This function echoes following:
If - 2014-10-18
If - 2014-10-19
Else - 2014-10-20
But echo of $today (last row in code) is
Today: 2014-10-19
So, what is wrong? Last $date in function is "2014-10-20" and this value is returning to $today, but $today shows different value. Any idea?

As kojiro pointed out in the comment, you do not assign the return value of the inner call to todayDate(). To change this, replace this line
todayDate($date);
with
$date = todayDate($date);

Related

PHP check if date is expired

i have an Array with this string value.
[VALIDUNTIL] => 28.08.23
Now i need to compare it with the todays date and check if the VALIDUNTIL date is in the same month and year as the today´s date
i looked for a while now but i cant find any solution.
while ($row = oci_fetch_assoc($dbResult))
{
$data[] = $row;
}
foreach($data as $ar) {
if ($ar['VALIDUNTIL'] && $ar['VALIDUNTIL'] != null) {
if(strtotime($ar['VALIDUNTIL']) >= strtotime($date)) {
}
}
When i try to do it with timestamps it cant read the VALIDUNTIL date and gives me the 1970 timestamp
strtotime seems not to work too.
tnx 4 help.
while ($row = oci_fetch_assoc($dbResult))
{
if(!empty($row['VALIDUNTIL']) && !is_null($row['VALIDUNTIL'])){
$valid_date = date('d-m-Y', strtotime($row['VALIDUNTIL']));
$current_date = date('d-m-Y');
if($valid_date >= $current_date ) {
echo "True";
}
}
}
Here are the PHP string conversions for time ref. https://www.w3schools.com/php/php_ref_date.asp
echo(strtotime("now") . "<br>");
echo(strtotime("3 October 2005") . "<br>");
echo(strtotime("+5 hours") . "<br>");
echo(strtotime("+1 week") . "<br>");
echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>");
echo(strtotime("next Monday") . "");
echo(strtotime("last Sunday"));
You'd need to have both dates in the same format. The above is from : https://www.w3schools.com/php/func_date_strtotime.asp#:~:text=The%20strtotime()%20function%20parses,are%20mapped%20to%201970%2D2000.
if you echo (strtotime($ar['VALIDUNTIL']) and strtotime($date)) back to the screen then you can visually compare and check to see the format which you are using is the same for both dates and adjust accordingly.

I want to set the winning day of the event as a weekday, but is it possible to resolve the error?

// holiday array
$holy = [
'2020-12-23',
'2020-12-24',
'2020-12-25',
'2020-12-28',
'2020-12-29',
];
$inputDate = '2020-12-23'; // input
$outputDate = get_date($inputDate);
echo "Winning Day: " . $outputDate . "<br />";
echo "<br />";
function get_date($chkDate)
{
global $holy;
$chkDateYoil = date("w", strtotime($chkDate)); // sat(6), sun(0)
if ($chkDateYoil == 6) {
// Saturday when + 2
$timestamp = strtotime($chkDate . " +2 days");
$chkDate = date("Y-m-d", $timestamp);
} else if ($chkDateYoil == 0) {
// Sunday when + 1
$timestamp = strtotime($chkDate . " +1 days");
$chkDate = date("Y-m-d", $timestamp);
}
// If it's a weekday, compare it array
foreach ($holy as $key => $holyday) {
if ($chkDate == $holyday) {
// holiday when + 1
$day_plus = 8 - $chkDateYoil;
$timestamp = strtotime($chkDate." +".$day_plus." days");
$chkDate = date("Y-m-d", $timestamp);
}
}
return $chkDate;
}
Hello, let me ask a question.
The following codes are:
Is the input value weekend?
Or are they included in the array?
in accordance with the judgment
Weekday extraction code.
But there is an error.
in my estimation
December 30th is supposed to come out.
By the way, January 2, 2021 is the result.
Why is that?
sorry
i don't write english very well
Thank you for reading.
The problem is in this line: $day_plus = 8 - $chkDateYoil;, which is calculating the date of the next Monday the first time it's executed.
You're then looping through the rest of the $holy array, and updating $chkDate if necessary, but you're not recalculating the value of $chkDateYoil, so the output depends on the day of the week you run this. Today (23rd December) it stops on 2nd January
Your code can be simplified by just incrementing the date by 1 day and performing the checks again, continuing until you get a result. I've also used the PHP function in_array() to simplify the search of the $holy array, and incorporated it into the same test as Saturday and Sunday.
// holiday array
$holy = [
'2020-12-23',
'2020-12-24',
'2020-12-25',
'2020-12-28',
'2020-12-29',
];
$inputDate = '2020-12-23'; // input
$outputDate = get_date($inputDate);
echo "Winning Day: " . $outputDate . "<br />";
echo "<br />";
function get_date($chkDate)
{
global $holy;
do {
$chkDateYoil = date("w", strtotime($chkDate)); // sat(6), sun(0)
if (($chkDateYoil == 0) || ($chkDateYoil == 6) || (in_array($chkDate, $holy))) {
$timestamp = strtotime($chkDate . " +1 days");
$chkDate = date("Y-m-d", $timestamp);
} else {
return $chkDate;
}
} while (true);
}

Compare month in two custom date fields using datetime

our site does festival reviews, and for this we want to be able to select a start date (datum) and an end date (eind_datum) via date time picker custom fields. Ultimately we want a function that determines whether the start and end date have the some month, so we end up with something like this
'1-4 Juli 2020' (instead of '1 Juli - 4 Juli 2020).
When the month is different we want it to be displayed as follows
'30 Juni - 4 Juli 2020'.
I have tried several things but none seem to work, the month needs to be in Dutch, I tried setting the locale but that didn't help, and the following code below throws an error at this line
'$first = $startdateTime->format('F');'
Anybody out here who might help ?
<?php
$start_date = get_field('datum');
$end_date = get_field('eind_datum');
$startdatetime = DateTime::createFromFormat('Y-m-d H:i:s', $start_date);
$enddatetime = DateTime::createFromFormat('Y-m-d H:i:s', $end_date);
$first = $startdateTime->format('F');
$second = $enddateTime->format('F');
if (get_field('eind_datum') and get_field('datum') and ($first == $second )) {
echo $start_date->format ('j'); echo "-"; echo $end_date->format ('j F Y') ;
}
elseif (get_field('eind_datum') and get_field('datum') and ($first != $second )) {
echo $start_date->format('j F'); echo "-"; echo $end_date->format ('j F Y') ;
}
else {
echo the_field('datum') ;
}
?>
The following should work:
$start_date = get_field('datum');
$end_date = get_field('eind_datum');
$startdatetime = new DateTimeImmutable($start_date);
$enddatetime = new DateTimeImmutable($end_date);
$first = $startdatetime->format('F');
$second = $enddatetime->format('F');
if ($start_date and $end_date and ($first == $second )) {
echo $startdatetime->format ('j'); echo "-"; echo $enddatetime->format ('j F Y') ;
}
elseif ($end_date and $start_date and ($first != $second )) {
echo $startdatetime->format('j F'); echo "-"; echo $enddatetime->format ('j F Y') ;
}
else {
echo $start_date;
}
The problem in you code is that the variables are sometime called incorrectly. For example: $start_date is not an object, and you cannot call the method format on it... or $startdateTime is not defined it is rather $startdatetime
Instead of creating 2 formats under certain conditions, you can also create the format '1 July - 4 July 2020' and then remove the month with preg_replace () if the months are the same.
$dateStr = '1 Juli - 4 Juli 2020';
$dateStr = preg_replace('~^(\d+) (\w+) - (\d+) (\2)~',"$1-$3 $2",$dateStr);
If the months are unequal, nothing is removed.

How to calculate date in PHP using variables

Can anyone help create this logic? Kind of difficult to explain...
Looking to take a date, add 3 days then select the next date based on a database value.
Say we start with:
$end_date = "2017-08-23 23:59:59"
$payday = 5; //1=monday, 2=tuesday, 3=wednesday, 4=thursday, 5=friday
//And we want to calculate $paydate:
$temp_date = $end_date + 3 days;
$pay_date = the first $payday(day of week) after $temp_date
Any ideas how to write this in php? This one is stumping me. Thanks!
To add three days you can do this:
$date = new DateTime('2017-08-23 23:59:59');
$date->add(new DateInterval('P3D'));
$date->modify("next friday");
echo $date->format('Y-m-d') . "\n";
You could also use a lookup table, or array, that matches number to named days of the week and use something like $date->modify("next $days[$payday]");
where
$days = [ [1] => "monday",
.... etc
How about this? It will get a date, add 3 days, and then loop through the next days until it finds a day in the $days array (which you can get from a database):
<?php
$date = new DateTime('2017-08-23 23:59:59');
function nextPayday($date) {
$date->add(new DateInterval('P3D'));
echo "Date + 3 days: ".$date->format('D Y-m-d') . "<br>";
$payDate = $date->add(new DateInterval('P1D'));
$days = ["1", "2", "3", "4", "5"];
while (!in_array($payDate->format("N"), $days)) {
$payDate->add(new DateInterval('P1D'));
}
return $payDate->format("D Y-m-d");
}
echo "Date + 3 days: ".$date->format('D Y-m-d') . "<br>";
echo "Next payday: ".nextPayday($date);
Demo
Or, if you need to find a next specific day, use this function instead:
function nextPayday($date) {
$date->add(new DateInterval('P3D'));
echo "Date + 3 days: ".$date->format('D Y-m-d') . "<br>";
$payDate = $date->add(new DateInterval('P1D'));
$day = "5";
while ($payDate->format("N") !== $day) {
$payDate->add(new DateInterval('P1D'));
}
return $payDate->format("D Y-m-d");
}
Jeff, take a look at Carbon which extends PHP's DateTime object.
It allows you to deal with dates in a very clean and intuitive way. Based on your example:
$date = Carbon::parse('next monday')->toDateString();
Adjust that to your exact case.
Another version to achieve the objective:
function nextPayday($dateString, $paydayNum) {
$paydays = [
1=>'monday',
2=>'tuesday',
3=>'wednesday',
4=>'thursday',
5=>'friday'
];
$temp_date_stamp = date('d-m-Y H:i:s', strtotime($dateString.' +3 days'));
$pay_date_stamp = strtotime('first '.$paydays[$paydayNum].' '.$temp_date_stamp);
return date('d-m-Y H:i:s', $pay_date_stamp);
}
$end_date = "2017-08-23 23:59:59";
$payday = 5;
echo nextPayday($end_date, $payday);
result:
01-09-2017 23:59:59

how to get 4-days ago to now - php

If current date is 2016-03-06, I would like to get these dates :
2016-03-06
2016-03-05
2016-03-04
2016-03-03
I'm trying to get this purpose but my result not what I want :
$_4date = date("y-m-d",strtotime("day"));
$_3date = date("y-m-d",strtotime("-1 day"));
$_2date = date("y-m-d",strtotime("-2 day"));
$_1date = date("y-m-d",strtotime("-3 day"));
echo $_4date;
echo '<br />';
echo $_3date;
echo '<br />';
echo $_2date;
echo '<br />';
echo $_1date;
the result is :
70-01-01
16-03-05
16-03-04
16-03-03
To get today's date with strtotime, you do strtotime("today");. However, as Bjorn has commented, you can simply just call date() directly.
Furthermore, the reason you are not getting the year in four digits is because you are using a lowercase y instead of an uppercase Y.
Try date("Y-m-d", strtotime("-1 day"));.
The following piece of code illustrates the required changes:
$today = date("Y-m-d");
$yesterday = date("Y-m-d", strtotime("-1 day"));
echo "$today <br />";
echo "$yesterday <br />";
// Output
2016-03-06
2016-03-05
For more informtation, please consult the PHP documentation on the date function. It actually shows you that what to expect from y and Y and it also shows you that the default value that is passed as the second argument is time(), meaning the default is the current time.
PHP's strtotime documentation can be consulted for more information on the strtotime() function and its possible parameters.
Always check the (PHP) documentation first before asking a question.
You need to use like that:
$_4date = date("Y-m-d");
$_3date = date("Y-m-d",strtotime("-1 day"));
$_2date = date("Y-m-d",strtotime("-2 day"));
$_1date = date("Y-m-d",strtotime("-3 day"));
Explanation:
For current date no need to use use strtotime().
For full year you need to use this format Y-m-d.
y-m-d will return you the date 16-03-06 but Y-m-d will return you 2016-03-06.
Use a for loop with strtotime( "... days ago" ):
for( $i = 0; $i < 4; $i++ )
{
echo date( 'Y-m-d', strtotime( "$i days ago" ) ) . PHP_EOL;
}
The first loop (0 days ago) will output today date, other loops will output past days.
3v4l.org demo
You need to use capital 'Y' for full year (2016) instead of small 'y' which will display year in shorthand (16).
And for current date just use date("Y-m-d").
<?php
$_4date = date("Y-m-d");
$_3date = date("Y-m-d",strtotime("-1 day"));
$_2date = date("Y-m-d",strtotime("-2 day"));
$_1date = date("Y-m-d",strtotime("-3 day"));
echo $_4date;
echo '<br />';
echo $_3date;
echo '<br />';
echo $_2date;
echo '<br />';
echo $_1date;
?>
Working Example
<?php
$date = [date("Y-m-d")];
for($i = 1; $i < 4; $i++) {
$date[] = date("Y-m-d",strtotime("-$i day"));
}
//For cli output you'll need:
echo implode("\n", $date) . "\n";
//For web output you'll need:
echo implode("<br />", $date) . "<br />";

Categories