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
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 />";
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);
I'm currently using a website to get the time in Athens:
$d = new DateTime("now", new DateTimeZone("Europe/Athens"));
echo $d->format("l, d M Y");
But I would like the date to be displayed in Greek and in the same format.
The full answer:
date_default_timezone_set('Europe/Athens');
setlocale(LC_TIME, 'el_GR.UTF-8');
echo strftime('%A ');
$greekMonths = array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαΐου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου');
$greekDate = date('j') . ' ' . $greekMonths[intval(date('m'))-1] . ' ' . date('Y');
echo $greekDate;
this will display the date like:
Πέμπτη 4 Φεβρουαρίου 2013
UPDATE: For the above chunk to work it is very important to set your PHP locale to Greek.
This is an alternative:
date_default_timezone_set('Europe/Athens');
setlocale(LC_TIME, 'el_GR.UTF-8');
$day = date("w");
$greekDays = array( "Κυριακή", "Δευτέρα", "Τρίτη", "Τετάρτη", "Πέμπτη", "Παρασκευή", "Σάββατο" );
$greekMonths = array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαΐου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου');
$greekDate = $greekDays[$day] . ' ' . date('j') . ' ' . $greekMonths[intval(date('m'))-1] . ' ' . date('Y');
echo $greekDate;
I was in the quest to find the same thing but I didn't find a full solution as I needed it. In my case I need to write the posted datetime of the article in Greek like Αναρτήθηκε Σάββατο 2 Μαΐου 2015.
So using some code of costastg answer, I managed to put together the following function.
I am sure there are other solutions out there:
function formatToGreekDate($date){
//Expected date format yyyy-mm-dd hh:MM:ss
$greekMonths = array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαΐου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου');
$greekdays = array('Δευτέρα','Τρίτη','Τετάρτη','Πέμπτη','Παρασκευή','Σάββατο','Κυριακή');
$time = strtotime($date);
$newformat = date('Y-m-d',$time);
return $greekdays[date('N', strtotime($newformat))-1].' '. date('j', strtotime($newformat)).' '.$greekMonths[date('m', strtotime($newformat))-1]. ' '. date('Y', strtotime($newformat)); // . ' '. $date;
}
You should use setlocale and strftime, an example to get the idea:
setlocale(LC_TIME, 'el_GR.UTF-8');
// Your display formatting from https://www.php.net/manual/en/function.strftime.php
$field_date_format = '%A, %d %B, %Y';
// Will display= Greek: Πέμπτη, 14 Φεβρουάριος, 2013
echo "Greek: ".strftime($field_date_format, strtotime("14-02-2013"));
I know "Φεβρουάριος" is not "Φεβρουάριου" but if is not crucial to your needs you can use this.
If I'm right you want to display the l parameter in Greek. You should set your PHP locale to Greek then.
Use setlocale(): http://php.net/manual/en/function.setlocale.php
setlocale(LC_TIME, 'greek');
You should have everything you need using date:
$greekMonths = [
'Ianouarios', 'Fevrouarios', 'Martios', 'Aprilios', 'Maios',
'Iounios', 'Ioulios', 'Avgoustos', 'Septemvrios', 'Oktovrios',
'Noemvrios', 'Dekemvrios'
];
$greekDate = date('d') . ' ' . $greekMonths[intval(date('m'))-1] . ' ' . date('Y');
echo $greekDate;
Second way using setlocale:
setlocale(LC_CTYPE, 'greek');
setlocale(LC_TIME, 'greek');
A simple method that works for all formats
function date_greek($format,$time=null){
if($time===null)$time=time();
$date = date($format,$time);
if(strpos($format,'F')!==false){
$en_months = array('January','February','March','April','May','June','July','August','September','October','November','December');
$el_months = array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαΐου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου');
}else{
$en_months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$el_months = array('Ιαν','Φεβ','Μαρ','Απρ','Μαι','Ιουν','Ιουλ','Αυγ','Σεπ','Οκτ','Νοε','Δεκ');
}
$en_days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
$el_days = array('Δευτέρα','Τρίτη','Τετάρτη','Πέμπτη','Παρασκευή','Σάββατο','Κυριακή');
$en_days_s = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
$el_days_s = array('Δευ','Τρι','Τετ','Πεμ','Παρ','Σαβ','Κυρ');
$en_ampm = array('AM','PM');
$el_ampm = array('ΠΜ','ΜΜ');
$date_el = str_replace($en_months,$el_months,$date);
$date_el = str_replace($en_days,$el_days,$date_el);
$date_el = str_replace($en_days_s,$el_days_s,$date_el);
$date_el = str_replace($en_ampm,$el_ampm,$date_el);
return $date_el;
}
echo date_greek('l, d M Y',strtotime('2021-04-25')); // Πέμπτη, 22 Απρ 2021
echo date_greek('l j F Y h:i:s A',strtotime('2021-04-25')); // Πέμπτη 22 Απριλίου 2021 12:00:00 ΠΜ
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.