So I've written the for loop as shown below, and for some reason, after the first time through the loop, the loop stops. In this case, I am trying to send the value 4 through, and it fails the if statement that checks for a string "true" from the checkRes function. That is all that function does is return strings. It is my understanding of loops that it will keep going through the loop until the statement at the top is met, or it is exited.
What am I doing wrong below?
// assume $avaliable = 4;
for ($i=$avaliable;10>$i;$i++) {
$check = checkRes($i, $people_no, $booking_date);
if ($check === "true") {
switch($i) {
case 0: $newResTime = "6 PM"; break;
case 1: $newResTime = "6:30 PM"; break;
case 2: $newResTime = "7 PM"; break;
case 3: $newResTime = "7:30 PM"; break;
case 4: $newResTime = "8 PM"; break;
case 5: $newResTime = "8:30 PM"; break;
case 6: $newResTime = "9 PM"; break;
case 7: $newResTime = "9:30 PM"; break;
case 8: $newResTime = "10 PM"; break;
case 9: $newResTime = "10:30 PM"; break;
case 10: $newResTime = "11 PM"; break;
}
// Replace next line with your return from the chatbot
echo "We're sorry, that time isn't avaliable, but a reservation at $newResTime has been made!";
exit;
}
}
You are currently using exit as a "default" solution. But it always terminates execution of the program (since that is what exit does).
Instead do this
switch($i) {
case 0: $newResTime = "6 PM"; break;
case 1: $newResTime = "6:30 PM"; break;
case 2: $newResTime = "7 PM"; break;
case 3: $newResTime = "7:30 PM"; break;
case 4: $newResTime = "8 PM"; break;
case 5: $newResTime = "8:30 PM"; break;
case 6: $newResTime = "9 PM"; break;
case 7: $newResTime = "9:30 PM"; break;
case 8: $newResTime = "10 PM"; break;
case 9: $newResTime = "10:30 PM"; break;
case 10: $newResTime = "11 PM"; break;
default: echo "..."; break;
Related
Hi
i bought a project from someone ,when i run it i have a problem .
the problem is when i try to load match with CURL from this link http://www.planetwin365.com/ControlsSkin/OddsEvent.aspx?ShowLinkFastBet=0&showDate=1&showGQ=1&rnd=049759534356372304&EventID=7944&GroupSep=undefined
to my database i show me this problem
this is my code
private function get_date_time($date,$time){
if(!Auth::user()->hasRole(['correction', 'admin'])){
return redirect('/');
}
//sabato 4 giugno 2016
$dates = explode(' ',$date);
$times = str_replace('.',':',$time);
switch ($dates[2]){
case 'gennaio':
$month = 01;
break;
case 'febbraio':
$month = 02;
break;
case 'marzo':
$month = 03;
break;
case 'aprile':
$month = 04;
break;
case 'maggio':
$month = 05;
break;
case 'giugno':
$month = 06;
break;
case 'luglio':
$month = 07;
break;
case 'agosto':
$month = 8;
break;
case 'settembre':
$month = 9;
break;
case 'ottobre':
$month = 10;
break;
case 'novembre':
$month = 11;
break;
case 'dicembre':
$month = 12;
break;
default:
$month = 01;
}
//2016-04-24 05:09:03
return $dates[3].'-'.$month.'-'.$dates[1].' '.$times;
}
please help me
Looks like $dates array has only three elements. Please remember the first index in an array is 0 not 1. I cannot confirm it as this service doesn't work in my country, but the following should do the trick.
switch ($dates[1]){
//...
$day = str_replace('.','',$dates[0]);
return $dates[2].'-'.$month.'-'.$day.' '.$times;
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 want to create my own DateTime format. I've tried the next:
function getSpanishDate(\DateTime $date)
{
$day = date('d', $date);
$month = date('n', $date);
$year = date('Y', $date);
switch($month)
{
case 1:
$newMonth = 'Enero';
break;
case 2:
$newMonth = 'Febrero';
break;
case 3:
$newMonth = 'Marzo';
break;
case 4:
$newMonth = 'Abril';
break;
case 5:
$newMonth = 'Mayo';
break;
case 6:
$newMonth = 'Junio';
break;
case 7:
$newMonth = 'Julio';
break;
case 8:
$newMonth = 'Agosto';
break;
case 9:
$newMonth = 'Septiembre';
break;
case 10:
$newMonth = 'Octubre';
break;
case 11:
$newMonth = 'Noviembre';
break;
case 12:
$newMonth = 'Diciembre';
break;
}
return $day . ' de ' . $newMonth . ' de ' . $year;
}
but something is wrong with the $date format, because I'm getting this warning:
Warning: date() expects parameter 2 to be long, object given
So I should convert the DateTime object into a long, but how?
Don't both converting. You've already got a DateTime object, just its own already-provided formatting method: http://php.net/manual/en/datetime.format.php
$day = date('d', $date); // your bad version
$day = $date->format('d'); // use this instead.
You're using date so $date should be a UNIX timestamp rather than an object (DateTime).
http://php.net/manual/en/function.date.php
At the moment I am writing a calendar. According to the chosen motn ($monthnum), I store the abbreviated month name ($monthabbr) in a database. For that I use a switch-case construct. It works for all months, except for 08-August and 09-September. Since I used the same code for all months, I don't know why it's not working. I am close to the edge to start all over again, but before that I'll better ask if you see an error.
switch( $monthnum ) {
case 01:
$monthabbr = 'Jan';
break;
case 02:
$monthabbr = 'Feb';
break;
case 03:
$monthabbr = 'Mär';
break;
case 04:
$monthabbr = 'Apr';
break;
case 05:
$monthabbr = 'Mai';
break;
case 06:
$monthabbr = 'Jun';
break;
case 07:
$monthabbr = 'Jul';
break;
case 08:
$monthabbr = 'Aug';
break;
case 09:
$monthabbr = 'Sep';
break;
case 10:
$monthabbr = 'Okt';
break;
case 11:
$monthabbr = 'Nov';
break;
case 12:
$monthabbr = 'Dez';
break;
}
Change 01, 02, ..., 09 to just 1, 2, ..., 9 (drop the zeros).
By starting an integer literal with a 0 indicates that it should be interpreted as an octal number (a number in base 8).
For octal numbers the digits 8 and 9 are illegal.
Further reading:
PHP Manual: Integers
(Btw, you may want to consider using an array or a map from integer to string, and just look up the string using something like monthAbbrs[$monthnum])
You can avoid this issue by using quotes
switch($date){
case "01": $month = "January"; break;
case "02": $month = "February"; break;
case "03": $month = "March"; break;
case "04": $month = "April"; break;
case "05": $month = "May"; break;
case "06": $month = "June"; break;
case "07": $month = "July"; break;
case "08": $month = "August"; break;
case "09": $month = "September"; break;
case "10": $month = "October"; break;
case "11": $month = "November"; break;
case "12": $month = "December"; break;
}
I dont know what the purpose is but a better way is to parse your date
$mysqldate = date( 'Y-m-d H:i:s', $phpdate );
$phpdate = strtotime( $mysqldate );
see here
and then format it in the way you like:
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
see here
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