Setting strtotime adding a variable to amount of days - php

I am trying to export a certain number of days and setting it to the variable $max_future. Currently it is a set amount of days but I want the users input variable to be the number it has instead.
Currently it is set as:
$max_future = date("Y-m-d", strtotime($today . "+6 days"));
I want something like:
$exportDays = '9'; //(or whatever the user input was)
$max_future = date("Y-m-d", strtotime($today . "+$exportDays days"));
Is this possible? I appreciate any help

Try:
$max_future = date("Y-m-d", strtotime($today . "+" . $exportDays . " days"));

Something like this :
<?php
$output = 9; // you can take this value from input.
$today = date("Y-m-d");
echo date('Y-m-d', strtotime($today. ' +'.$output. ' days'));
?>

<?php
$exportDays = $_REQUEST['exportDays']; // or however you want to get it from user input
$max_date = date('Y-m-d', strtotime(date('Y-m-d') . ' +' . $exportDays . ' days'));

Related

How do I create a automatic "next delivery time " text line in wordpress php?

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

PHP What is wrong with recursive function?

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);

Date and time in Greek

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 ΠΜ

Function in code I'm debugging seems to not take into account shifts to and from DST

I'm debugging a conference room booking calendar which was put together by someone which is no longer working with me. It's been a nightmare since there were so many things wrong with it, but I'm having trouble figuring out exactly what's causing this last bug. The calendar checks to see if rooms have already been booked at certain times and they aren't having an issue showing as booked at the right time, but if someone tries to book the same room an hour or less after the room is vacated after a shift to or from DST, it shows the room as booked still.
Example:
User sees that a room is reserved for Nov. 29th from 9am-10am.
User then tries to book the room on Nov. 29th from 10:30am-12:00pm.
The calendar cancels this second request and informs the user that the room is already booked.
It should be noted that this doesn't happen any time before the shift to DST (Nov. 4th). Here's the function which determines if the room is available:
function calCheck($starttime, $endtime, $cal_name, $cat_id, $myDB, $myHost, $myUser, $myPass) {
$timezone = 'America/Denver';
date_default_timezone_set ($timezone);
$dset = new DateTime($odate, new DateTimeZone($timezone));
$dset2 = $dset->getOffset();
//$starttime = $starttime + 1;
//$endtime = $endtime - 1;
$starttime = $starttime - $dset2 + 1;
$endtime = $endtime - $dset2 - 1;
$starttime = $starttime;
$endtime = $endtime;
//echo $starttime .'</br>'. $endtime . '</br>';
$db = new myDB($myDB, $myHost, $myUser, $myPass);
$db->myDB_Connect();
//echo 'calcheck</br>';
$ck_query = 'SELECT * FROM vw_cal_chk
WHERE (stime < '. $starttime . ' AND etime > ' . $starttime . ' ) and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"
OR (stime < ' . $endtime . ' AND etime > ' . $endtime . ') and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"
OR (stime >= '. $starttime . ' AND etime < ' . $endtime .') and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"';
$ck_result = $db->myQuery($ck_query);
$num = mysql_num_rows($ck_result);
//echo $ck_query . '</br>' . $num;
if ($num >> 0){
$avail = 1;
} else {
$avail = 0;
}
return $avail;
}
All of my timestamps up to this point are in UTC and I notice the $odate variable is actually never instantiated anywhere, but I haven't been able to determine what value to pass it in order for the offsetting to work correctly. If I can find out what kind of date it wants, I should be able to work the rest out.
$odate is null which defaults to now giving you the current offset. What you want is the offset at the time that the schedule is being set not the offset right at this moment. It looks like your database dates are in Mountain time and that your startdate is utc and you are subtracting the offset to get back to mountain (I would think that you would add the offsets not subtract , so I might have this reversed, can you query your database and figure it out?)
In any event you should be computing your offset using $startdate not now(), try switching $startdate for $odate and see what happens. If this doesn't work try building a string from startdate and then generating a new date from that string. If nothing else outputting that string should give you a clear picture as to whether $startdate is UTC or mountain

How to show numerical date in mm/dd/yy format

Right now i am the month, day, and year of a user seperated in three db fields titled month, day, year.
When i display it i am doing:
$month = $row['month'];
$day = $row['day'];
$year = $row['year'];
then to echo it:
$month/$day/$year
The problem is that the PHP is doing mathematics here and dividing the numbers... What can i do to not make that happen and let it simply display the dates..
Thanks
try this out :
echo "{$month}/{$day}/{$year}";
echo $month.'/'.$day.'/'.$year;
date('m/d/Y',strtotime($month . ' ' . $day . ' ' . $year));
The advantage of this is that you can choose how to format the date independent of how it is stored in your database: http://php.net/manual/en/function.date.php
echo $month . "/" . $day . "/" . $year;
Doing string concatenation.
or
echo "{$month}/{$day}/{$year}";
Doing string interpolation.
See the difference/performance of the two here.

Categories