Need to add a specified numbers of days to a selected date. Not the current date. Im using strtotime in this fashion:
$date = $_POST['r_date'];
$r_date = $date;
$txts_rental = $_POST['txts_rental'];
switch ($txts_rental){
case "250.00":
$s_length = "30";
break;
case "575.00":
$s_length = "90";
break;
case "975.00":
$s_length = "180";
break;
case "1200.00":
$s_length = "365";
break;
}
$rn_date = date( "Y-m-d", strtotime( "$s_length day" ) );
the date( "Y-m-d" needs to be the the $r_date variable in order to get the renewal date from the inputted date
You can use
$EndDateTime = DateTime::createFromFormat('d/m/Y', "16/07/2017");
$EndDateTime->modify('+6 days');
echo $EndDateTime->format('d/m/Y');
or
$today = "2015-06-15"; // Or can put $today = date ("Y-m-d");
$fiveDays = date ("Y-m-d", strtotime ($today ."+5 days"));
You need to change your method as below. add the target date in second parameter so that the date not added in current date
$date = $_POST['r_date'];
$r_date = $date;
$txts_rental = $_POST['txts_rental'];
switch ($txts_rental){
case "250.00":
$s_length = "30";
break;
case "575.00":
$s_length = "90";
break;
case "975.00":
$s_length = "180";
break;
case "1200.00":
$s_length = "365";
break;
}
$rn_date = date( "Y-m-d", strtotime( "$date +".$s_length." day" ) );
Related
I want to get an array of dates within a period. To do so I thought up a for loop (seems simple enough...) But when I run it even with for the dates of 1 month it times out.
This is my php:
$startdate = '2018-01-31';
$recurring = '2';
switch($recurring) {
case '1':
$period = '+1 day';
break;
case '2':
$period = '+1 week';
break;
case '3':
$period = '+1 month';
break;
case '4':
$period = '+3 months';
break;
case '5':
$perion = '+1 year';
break;
default:
$period = null;
break;
}
$dates = [];
if($period !== null) {
for($date = $startdate; $date < strtotime('+1 month', $startdate); strtotime($period, $date)) {
$dates[] = $date;
}
}
echo json_encode($dates);
Increasing $date with $date = strtotime($period, $date) in the increment part of the for loop should keep it from timing out, but there are a couple of other improvements that could be made.
First I'd recommend calculating your end date once before the loop to avoid extra strtotime calls each time it checks the continuation condition.
$end = strtotime("$startdate +1 month");
Then, set $date = strtotime($startdate) in the initialization part, or you'll get a date string instead of a timestamp as the first value in your $dates array.
for ($date = strtotime($startdate); $date < $end; $date = strtotime($period, $date)) {
$dates[] = $date;
}
I've a Date-Ouput and format-string in my Query:
$date = $sql->getValue("date");
$datenew = DateTime::createFromFormat('Y-m-d', $date);
$dateex = $datenew->format('l, d.m.Y');
// Output: Monday, 19.12.2016
Is there a way to get the "Monday" in german ("Montag")?
Yes. You can use setlocale() :
setlocale(LC_TIME, 'de_DE', 'deu_deu');
$date = date('l, d.m.Y');
echo $date; //outputs: Montag, 19.12.2016
Have a look at: http://php.net/manual/en/function.setlocale.php
Or you could use a workaround like:
$date = date('l, d.m.Y');
$arrDate = explode(",",$date);
$weekDay = "";
switch($arrDate[0])
{
case 'Monday': $weekDay = 'Montag, ';
break;
case 'Tuesday': $weekDay = 'Dienstag, ';
break;
case 'Wednesday': $weekDay = 'Mittwoch, ';
break;
.
.
.
case 'Sunday' : $weekDay = 'Sonntag, ';
break;
}
echo $weekDay.$arrDate[1];
I would like to write a method which I can give a period of time (Like: yearly, monthly...) and it returns me an anterior date according to this period of time given.
Here is my code:
public function callRuleCeilling($period)
{
$start = new \DateTime();
switch ($period) {
case 'weekly':
$dateInterval = 'P7D';
break;
case 'monthly':
$dateInterval = 'P1M';
break;
case 'quaterly':
$dateInterval = 'P3M';
break;
case 'half-yearly':
$dateInterval = 'P6M';
break;
case 'yearly':
$dateInterval = 'P1Y';
break;
default:
$dateInterval = 'P1Y';
}
$start->sub(new \DateInterval($dateInterval));
return $start
}
My example problem:
If I put a starting date in the middle of the year with a yearly period. I want it to stop at the beginning of the year.
And I would like the same for monthly period (Stop at the beginning of the month) etc...
Does it exist a PHP function with do that? I can't find it.
Please highlight me.
Thanks fo the highlight. It allowed me to did that way:
public function callRuleCeilling($period)
{
$start = new \DateTime();
$month = 'January';
switch ($period) {
case 'weekly':
$timestampMonday = strtotime('last monday', strtotime('tomorrow'));
$start = $start->setTimestamp($timestampMonday);
break;
case 'monthly':
$month = $start->format('F');
$start = new \DateTime('first day of '.$month);
break;
case 'quaterly':
$monthNumber = $start->format('n');
if($monthNumber >= 1) $month = 'January';
if($monthNumber >= 5) $month = 'May';
if($monthNumber >= 9) $month = 'September';
$start = new \DateTime('first day of '.$month);
break;
case 'half-yearly':
$monthNumber = $start->format('n');
if($monthNumber >= 1) $month = 'January';
if($monthNumber >= 7) $month = 'July';
$start = new \DateTime('first day of '.$month);
break;
case 'yearly':
$start = new \DateTime('first day of January');
break;
default:
$start = new \DateTime('first day of January');
}
return $start;
}
Im selecting a date in my input date with jQuery datepicker.
The format that Im using in datepicker script is: dateFormat: 'DD, d MM, yy',
So I get this date in my input: "Quinta-feira, 1 Maio, 2014" (portuguese date).
But now I need to convert this date to save like datetime in mysql.
I needed only to use this code below if the date was in english:
$date = DateTime::createFromFormat('l, j F, Y', $_POST['date']);
echo $date->format('Y-m-d');
But my date isnt english so I need to do the conversion and Im trying to use the function below" convertDate" to do this.
But when I call the function, and I pass the input date value like this: convertDate($_POST['date']);
I got an error "Call to a member function format() on a non-object in this line: $day= $date->format("l");
Do you see something wrong here? Because the fucntion seems good for me!
function convertDate($myDate){
$date = DateTime::createFromFormat('Ymd', $myDate);
$day = $date->format("l");
$daynum = $date->format("j");
$month = $date->format("F");
$year = $date->format("Y");
switch($day)
{
case "Segunda-Feira": $day = "Monday"; break;
case "Terça-Feira": $day = "Tuesday"; break;
case "Quarta-Feira": $day = "Wednesday"; break;
case "Quinta-Feira": $day = "Thursday"; break;
case "Sexta-Feira": $day = "Friday"; break;
case "Sábado": $day = "Saturday"; break;
case "Domingo": $day = "Sunday"; break;
default: $day = "Unknown"; break;
}
switch($month)
{
case "Janeiro": $month = "January"; break;
case "Fevereiro": $month = "February"; break;
case "Março": $month = "March"; break;
case "Abril": $month = "April"; break;
case "Maio": $month = "May"; break;
case "Junho": $month = "June"; break;
case "Julho": $month = "July"; break;
case "Agosto": $month = "August"; break;
case "Setembro": $month = "September"; break;
case "Outubro": $month = "October"; break;
case "Novembro": $month = "November"; break;
case "Dezembro": $month = "December"; break;
default: $month = "Unknown"; break;
}
echo $daynum . ", " . $month . ", " . $year;
}
Trying with str_ireplace:
$english = array("Segunda-Feira","Terça-Feira","Quarta-Feira","Quinta-Feira","Sexta-Feira","Sábado","Domingo");
$portuguese = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$result= str_ireplace ($english , $portuguese, $_POST['date']);
$date = DateTime::createFromFormat('l, j F, Y', $result);
echo $date->format('Y-m-d');
I got same error in echo $date->format('Y-m-d')
Instead of trying to translate dates in words in possibly various languages, you can have JQuery drop a UTC of sorts in a hidden form field and work with this instead:
This will return a JS date object:
var currentDate = $( ".selector" ).datepicker( "getDate" );
This will make a PHP UTC from it (it's microseconds in JS...)
var phputc = Math.ceil((currentDate.getTime()/1000));
This will put in in a hidden form:
$(".selector" ).change(
function()
{
var currentDate = $( ".selector" ).datepicker( "getDate" );
var phputc = Math.ceil((currentDate.getTime()/1000));
document.forms[0].nameofhiddenformfield.value = phputc;
}
);
Make sure there is a field like this in your form
<input type="hidden" name="nameofhiddenformfield" />
And on the PHP side you can then do:
$dateUTC = $_POST['nameofhiddenformfield'];
echo date('Y-m-d',$dateUTC);
Your approach is wrong, in the first line of your function you try to convert your Portuguese date as if it is in the Ymd format so your $date object is flawed to begin with.
What you should probably do (assuming that there is no localized function to convert Portuguese dates to DateTime...), is use something like a string replace to replace all Portuguese words with English words and after that use your original conversion:
$date = DateTime::createFromFormat('l, j F, Y', $translated_date);
The 17 / 34 words concerned don't interfere with each other so str_ireplace() with two arrays should do it.
I am digging in the code this professional programmer had put together for an events website for me and I found this function that seems super confusing, as it looks like 1 line of code with "strtotime" would do the same thing.
Is it because we wanted to use "Jan 2nd" format with the two letters after the day of the month?
function convert_date_to_web($date) {
if (empty($date) || $date == "0000-00-00") return $date;
switch ($date[5].$date[6]) {
case '01':
$month = 'Jan';
break;
case '02':
$month = 'Feb';
break;
case '03':
$month = 'Mar';
break;
case '04':
$month = 'Apr';
break;
case '05':
$month = 'May';
break;
case '06':
$month = 'Jun';
break;
case '07':
$month = 'Jul';
break;
case '08':
$month = 'Aug';
break;
case '09':
$month = 'Sep';
break;
case '10':
$month = 'Oct';
break;
case '11':
$month = 'Nov';
break;
case '12':
$month = 'Dec';
}
if($date[9] == "1") {
$day = $date[9]."st";
}
else if ($date[9] == "2") {
$day = $date[9]."nd";
}
else if ($date[9] == "3") {
$day = $date[9]."rd";
}
else {
$day = $date[9]."th";
}
if($date[8] != "0") {
$day = $date[8].$day;
}
$date = $month." ".$day.", ".$date[0].$date[1].$date[2].$date[3];
return $date;
}
And my version is
$timestamp = strtotime($event->start_date);
$start_date = date("M d, Y", $timestamp);
Here is an exact replicate of the original function. I've used two versions, the first uses strtotime, the second uses explode to avoid using strtotime.
$date = '2011-10-01';
$date_format = 'M jS, Y'; // Three letter month, day name w/o leading zero, day suffix, year
echo date( $date_format, strtotime( $date)) . "\n";
$date_pieces = explode( '-', $date);
echo date( $date_format, mktime( 0, 0, 0, $date_pieces[1], $date_pieces[2], $date_pieces[0]));
Demo
Yup. I think that is what the reason for that long function is. It is to add in the "st", "nd", "rd", and the "th".
Your code does not do that. Maybe you can try it this way? (Needs debugging, cos i never tested it on actual php)
$timestamp = strtotime($event->start_date);
$start_date = date("M d", $timestamp);
$lastNum = substr($start_date, -1)
switch($lastNum) {
case "0":
break;
case "1":
$start_date .= "st"; break;
case "2":
$start_date .= "nd"; break;
case "3":
$start_date .= "rd"; break;
default:
$start_date .= "th"; break;
}
$start_date .= date(", Y", $timestamp);
The reason why I prefer such explode()ish solutions over strtotime(), too, is that they give deterministic, portable results that are less surprising while strtotime() does some complex magic.
As with all programming there is more than one way to do something. Experience teaches programmer to do things a certain way. From the look of this this function if for converting the default output format of mysql dates into a version that converts the month to a string. It looks good to me