error get date time laravel - php

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;

Related

PHP DateInterval. Go back to the beginning of the year

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

Create custom date format

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

Calculate a due date in PHP

One of my co-workers recently installed an open-source script into our billing system. The purpose is to re-calculate the next due date for a service when the customer's service is paid for late. Example being service is due 11/20/2012, is suspended the next day, and then they pay for it 11/25/2012. This script is supposed to update the next due date to 12/25/2012, but is malfunctioning and changing (monthly) dates that should be 12/25/2012 to 1/25/2013 for some reason.
I think the issue is with the way the $month variable is being handled, but can't quite figure out where the issue is.
function calculate_postpone_due_date($billingcycle)
{
switch($billingcycle)
{
case "Monthly": $months = 1; break;
case "Quarterly": $months = 3; break;
case "Semi-Annually": $months = 6; break;
case "Annually": $months = 12; break;
case "Biennially": $months = 24; break;
case "Triennially": $months = 32; break;
default: $months = 0; break;
}
//we return FALSE for any other billing cycles: "One Time", "Free Account" etc, they are not recurring
if ($months == 0)
return FALSE;
//a bit complex calculation based on day of the month
//exactly like native whmcs logic do
$year = date("Y");
$month = date("m");
$day = date("d");
for ($i=1; $i<=$months; $i++)
{
$month++;
if ($month == 13)
{
$month = 1;
$year++;
}
}
if (checkdate("$month", $day, $year))
{
return "$year-$month-$day";
}
else
{
//getting last day of the month
$last_day = date("t", mktime(0, 0, 0, $month, 1, $year));
return "$year-$month-$last_day";
}
}
function calculate_postpone_due_date($billingcycle)
{
switch($billingcycle)
{
case "Monthly": $months = 1; break;
case "Quarterly": $months = 3; break;
case "Semi-Annually": $months = 6; break;
case "Annually": $months = 12; break;
case "Biennially": $months = 24; break;
case "Triennially": $months = 36; break;
default: $months = 0; break;
}
if ($months == 0)
return FALSE;
$today = date('Y-m-d');
$next_due_date = strtotime($today.' + '.$months.' Months');
return date('Y-m-d', $next_due_date);
}
Try this (not tested):
function calculate_postpone_due_date($billingcycle)
{
switch($billingcycle)
{
case "Monthly": $months = 1; break;
case "Quarterly": $months = 3; break;
case "Semi-Annually": $months = 6; break;
case "Annually": $months = 12; break;
case "Biennially": $months = 24; break;
case "Triennially": $months = 32; break;
default: return FALSE;
}
$expires = new DateTime('plus '.$months.' months');
$expires->modify('last day of this month');
return $expires->format('Y-m-d');
}
function countDueDate($policy_start_date,$months){
$next_due_date = strtotime($policy_start_date.' + '.$months.' Months');
if($next_due_date<time()){
return countDueDate(date('Y-m-d', $next_due_date), $months);
}else{
return date('Y-m-d',$next_due_date);
}
}
function getModeMonth($premium_mode){
switch ($premium_mode){
case 'yearly': $q=12;break;
case 'monthly': $q=1;break;
case 'quarterly': $q=3;break;
case 'half year': $q=6;break;
default : $q=12;break;
}
return $q;
}
$date=countDueDate(date('Y').'-'.date('m-d',strtotime($policy_start_date)), getModeMonth($premium_mode));

Switch Case - not all cases work

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

strtotime instead of this very long function?

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

Categories