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 />";
Related
So I have been coding a little custom line on my wordpress site using php, that should show the next delivery time automatically.
Right now it shows 2 automatically updating dates for 2 of the week days.
What I would want is them to cycle, so if it's less then 24 hours till lets say the fridays delivery you can't see that text. It would only show the other option.
This is the code that I currently have.
`
<p class="mb-4 fw-bold"><?php
_e('Järgmised tarneajad', 'esahver');
echo ": " ;
$now = strtotime("now");
$next_delivery = strtotime('next tuesday');
$limit_hours = 17;
$next_delivery_minus_hrs = strtotime('-' . $limit_hours . ' hours', $next_delivery);
$final_next_delivery = NULL;
if( $next_delivery_minus_hrs < $now ){ // past limit hours
$next2_delivery = strtotime('next tuesday', $next_delivery);
$final_next_delivery = wp_date( 'l, d.m.Y' , $next2_delivery);
} else {
$final_next_delivery = wp_date( 'l, d.m.Y' , $next_delivery );
}
$now1 = strtotime("now");
$next_delivery1 = strtotime('next friday');
$limit_hours1 = 17;
$next_delivery_minus_hrs1 = strtotime('-' . $limit_hours1 . ' hours', $next_delivery1);
$final_next_delivery1 = NULL;
if( $next_delivery_minus_hrs1 < $now ){ // past limit hours
$next2_delivery1 = strtotime('next friday', $next_delivery1);
$final_next_delivery1 = wp_date( 'l, d.m.Y' , $next2_delivery1);
} else {
$final_next_delivery1 = wp_date( 'l, d.m.Y' , $next_delivery1 );
}
echo $final_next_delivery;
echo "<input type='hidden' name='custom_delivery_date' value='".$final_next_delivery."'/>";
echo " & ";
echo $final_next_delivery1;
echo "<input type='hidden' name='custom_delivery_date' value='".$final_next_delivery1."'/>";
?>
</p>
`
How would I approach this problem?
Thanks
I have tried writing some bad if sentences that didn't work out at all.
You can refactor the code even more simply
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
I'm trying to use one's date of birth to calculate when he'll be 50, if he's not 50 already.
If person is not 50, add a year to his age then check if it'll be 50. If not, iterate until it's true. Then get the date he turned 50. in PHP
Here's the code, not complete.
$rAge = 50;
$retir = date('j F Y ', strtotime("+30 days"));
$oneMonthAdded = strtotime(date("d-m-Y", strtotime($DOB)). "+1 year");
$re = date("d-m-Y", $oneMonthAdded);
$futDate = date("d-m-Y", strtotime(date("d-m-Y", strtotime($re))));
$date_diff = strtotime($futDate)-strtotime($DOB);
$future_age = floor(($date_diff)/(60*60*24*365));
Help please.
try this code bro!
// your date of birth
$dateOfBirth = '1950-11-26';
// date when he'll turn 50
$dateToFifty = date('Y-m-d', strtotime($dateOfBirth . '+50 Years'));
// current date
$currentDate = date('Y-m-d');
$result = 'retired';
// checks if already fifty
if($currentDate <= $dateToFifty) {
$result = $dateToFifty;
}
echo $result;
I use simplest php code to find out retire date. y
<?php
$dob = '1970-02-01';
$dob_ex = explode("-",$dob);
$age_diff = date_diff(date_create($dob), date_create('today'))->y;
$year_of_retire = 50 - $age_diff;
$end = date('Y', strtotime('+'.$year_of_retire.'years'));
$date_of_retire = $end."-".$dob_ex[1]."-".$dob_ex[2];
echo $date_of_retire;
?>
you can use if...else condition to echo values according to you.
like
if($year_of_retire > 0){
echo $date_of_retire;
} else if($year_of_retire < 0){
echo "retired";
}
How do I get the "UTC, PST, EST, etc.." name from this time offset?
You can copy/paste this in any PHP file and it will run.
<?php
date_default_timezone_set('UTC');
$time = '2012-01-01T21:15:00+00:00';
echo '<pre>';
echo "<b>Step 1 (Have the date now)</b>\n";
$time = explode('T', $time);
print_r($time);
echo "\n\n<b>Step 2 (Get the offset)</b>\n";
$offset = explode('+', $time[1]);
$time[1] = $offset[0];
print_r($offset);
echo "\n\n";
echo date('l F jS', strtotime($time[0])) . ', at ' . date('g:ia', strtotime($time[1]));
echo "\n\n";
//
// How do I matchup the offset in this list?
// The values are -18000, -7200
//
$b = timezone_abbreviations_list();
print_r($b);
Note: I've also tried using echo date('c', $time); but perhaps I have something wrong its giving me an error.
In PHP 5.1.0+ echo date('e', $time); will return the timezone information.
This should be a simple fix;
I currently have a calendar for selecting a range of date:
http://protekco.com/index.php/en/reservations/1718-carvelle-drive-westpalm-beach.html
When the dates are selected in the calendar, it populates 2 input fields for Check in and Check out dates. The problem is, the calendar initially was set 1 day late, so Thursday Sept 22 actually showed as a Thursday Sept 21. I was able to just echo a +1 on the day count, but the input boxes still show the erroneous date. Essentially, I'm trying to add the same +1, but because the date is returned as yyyy/mm/dd, +1 doesn't do much.
Here is the code:
$listsFrom = $this->lists['from'];
$selectedFrom = $listsFrom ? strtotime($listsFrom) : 0;
$listsTo = $this->lists['to'];
$selectedTo = $listsTo ? strtotime($listsTo) : $selectedFrom;
if ($selectedTo) {
ADocument::addDomreadyEvent('Calendars.checkOut = ' . $selectedTo . ';');
}
if ($selectedFrom) {
ADocument::addDomreadyEvent('Calendars.checkIn = ' . $selectedFrom . ';');
}
$listOperation = $this->lists['operation'];
ADocument::addDomreadyEvent('Calendars.operation = ' . $listOperation . ';');
Any ideas?
Thank you!
Edit:
<td class="<?php echo implode(' ', $class); ?>" <?php if ($canReserveBox) { ?> id="day<?php echo $firstDay->Uts+84600000; ?>" onclick="Calendars.setCheckDate('day<?php echo $firstDay->Uts+84600000; ?>','<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_MYSQL_DATE, 0); ?>','<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_NORMAL); ?>')"<?php } ?> >
<span class="date" >
<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_NICE_SHORT)+1;
?>
</span>
This is what actually calls the changes. The + 84600000 is my addition, it doesn't seem to do much tho.
If the date is in yyyy/mm/dd format, first use strtotime() to convert it to a timestamp, and then you can use it again to add one day.
$date = '2011/06/01';
$ts = strtotime('+24 hours', strtotime($date));
$date = date('Y/m/d', $ts);
That is just one possible solution.