Архив

Архив раздела ‘PHP’

php:

class Calendar{
	public static function getMonth($month,$year,$events=array()){
		$months=array(
			1=>'Январь',
			2=>'Февраль',
			3=>'Март',
			4=>'Апрель',
			5=>'Май',
			6=>'Июнь',
			7=>'Июль',
			8=>'Август',
			9=>'Сентябрь',
			10=>'Октябрь',
			11=>'Ноябрь',
			12=>'Декабрь'
		);
		$month=intval($month);
		$out='
		<div class="calendar-item">
			<div class="calendar-head">'.$months[$month].' '.$year.'</div>
			<table>
				<tr>
					<th>Пн</th>
					<th>Вт</th>
					<th>Ср</th>
					<th>Чт</th>
					<th>Пт</th>
					<th>Сб</th>
					<th>Вс</th>
				</tr>';
		$day_week=date('N',mktime(0,0,0,$month,1,$year));
		$day_week--;
		$out.='<tr>';
		for($x=0;$x<$day_week;$x++){
			$out.='<td></td>';
		}
		$days_counter=0;
		$days_month=date('t',mktime(0,0,0,$month,1,$year));
		for($day=1;$day<=$days_month;$day++){
			$class='';
			$month_url=($month<10)?'0'.$month:$month;
			$day_url=($day<10)?'0'.$day:$day;
			if((!isset($_GET['date'])&&date('j.n.Y')==$day.'.'.$month.'.'.$year)||($_GET['date']==$year.'-'.$month_url.'-'.$day_url)){
				$class.=' today';
			}
			if(strtotime('-1 day')>strtotime($day.'.'.$month.'.'.$year)){
				$class.=' last';
			}
			$event_show=false;
			$event_text=array();
			if(!empty($events)){
				foreach($events as $date=>$text){
					$date=explode('.',$date);
					if(count($date)==3){
						$y=explode(' ',$date[2]);
						if(count($y)==2){
							$date[2]=$y[0];
						}
						if($day==intval($date[0])&&$month==intval($date[1])&&$year==$date[2]){
							$event_show=true;
							$event_text[]=$text;
						}
					}
					elseif(count($date)==2){
						if($day==intval($date[0])&&$month==intval($date[1])){
							$event_show=true;
							$event_text[]=$text;
						}
					}
					elseif($day==intval($date[0])){
						$event_show=true;
						$event_text[]=$text;
					}
				}
			}
			if($event_show){
				$class.=' event';
			}
			$out.='<td class="calendar-day'.$class.'"><a href="'.ADMIN_URL.'?work=schedule&date='.$year.'-'.$month_url.'-'.$day_url.'">'.$day.'</a>';
			if($event_show){
				if(!empty($event_text)){
					$out.='<div class="calendar-popup"><div>'.implode('</div><div>',$event_text).'</div></div>';
				}
			}
			$out.='</td>';
			if($day_week==6){
				$out.='</tr>';
				if(($days_counter+1)!=$days_month){
					$out.='<tr>';
				}
				$day_week=-1;
			}
			$day_week++;
			$days_counter++;
		}
		$out.='</tr></table></div>';
		return $out;
	}
	public static function getInterval($start,$end,$events=array()){
		$curent=explode('.',$start);
		$curent[0]=intval($curent[0]);
		$end=explode('.',$end);
		$end[0]=intval($end[0]);
		$begin=true;
		$out='<div class="calendar-wrp">';
		do{
			$out.=self::getMonth($curent[0],$curent[1],$events);
			if($curent[0]==$end[0]&&$curent[1]==$end[1]){
				$begin=false;
			}
			$curent[0]++;
			if($curent[0]==13){
				$curent[0]=1;
				$curent[1]++;
			}
		}
		while($begin==true);
		$out.='</div>';
		return $out;
	}
}

css:

.calendar-item{
	width:200px;
	display:inline-block;
	vertical-align:top;
	margin:0 15px 20px;
}
.calendar-head{
	text-align:center;
	padding:5px;
	font-weight:700;
	font-size:14px;
}
.calendar-item table{
	border-collapse:collapse;
	width:100%;
}
.calendar-item th,.calendar-item td{
	font-size:12px;
	text-align:center;
}
.calendar-item th{
	padding:5px;
	color:#888888;
	font-weight:400;
}
.calendar-item td{
	border:1px solid #dddddd;
}
.calendar-item td a{
	text-decoration:none;
	padding:5px;
	display:block;
}
.calendar-item td a:hover{
	background:#dddddd;
}
.calendar-item tr th:nth-child(6) a,.calendar-item tr th:nth-child(7) a,
.calendar-item tr td:nth-child(6) a,.calendar-item tr td:nth-child(7) a{
	color:#e65a5a;
}
.calendar-day.last a{
	color:#999999 !important;
}
.calendar-day.event{
	position:relative;
	cursor:pointer;
}
.calendar-day.event a{
	background:#e6f3fe;
}
.calendar-day.today a{
	background:#fe9c4b;
	color:#ffffff;
}
.calendar-day.event:hover .calendar-popup{
	display:block;
}
.calendar-popup{
	display:none;
	position:absolute;
	top:40px;
	left:0;
	min-width:200px;
	padding:15px;
	background:#ffffff;
	text-align:left;
	font-size:12px;
	z-index:100;
	box-shadow:0 0 10px rgba(0,0,0,0.5);
	color:#000000;
}
.calendar-popup div{
	margin-bottom:5px;
}
.calendar-popup:before{
	content:"";
	border:solid transparent;
	position:absolute;
	left:8px;
	bottom:100%;
	border-bottom-color:#ffffff;
	border-width:9px;
	margin-left:0;
}

Использование:
Текущий месяц

echo Calendar::getMonth(date('n'), date('Y'));

3 месяца

echo Calendar::getInterval(date('n.Y'), date('n.Y', strtotime('+2 month')));

Текущий год

echo Calendar::getInterval(date('01.Y'), date('12.Y'));

12 месяцев, начиная с текущего, с метками
формат даты:
d – день месяца, при таком формате, заданный день будет выделятся каждый месяц.
d.m – день и месяц, такая дата будет выделятся раз в год.
d.m.Y или d.m.Y H:i – точная дата.

$events=array(
	'15'=>'Заплатить ипотеку',
	'23.02'=>'День защитника Отечества',
	'08.03'=>'Международный женский день',
	'31.12'=>'Новый год'
);
echo Calendar::getInterval(date('n.Y'), date('n.Y', strtotime('+11 month')), $events);

На основе https://snipp.ru/php/php-calendar

Categories: PHP Tags:
$timeout=5;
$send=array(
	'k'=>'v'
);
$opts=array(
	'http'=>array(
		'method'=>'POST',
		'header'=>'Content-Type: application/json; charset=utf-8',
		'content'=>json_encode($send),
		'timeout'=>$timeout
	)
);
$context=stream_context_create($opts);
$result=file_get_contents('https://example.com/url',false,$context);
preg_match('/([0-9])\d+/',$http_response_header[0],$matches);
$responsecode=intval($matches[0]);
if($responsecode==200){
}
Categories: PHP Tags:
echo preg_replace('#<a.*?>(.*?)</a>#i','\1',$text);
Categories: PHP Tags:
21 декабря 2021 Нет комментариев
function phone_format($phone){
	$d=preg_replace('~\D+~','',$phone);
	if(strlen($d)>=10){
		$d10=substr($d,-10,10);
		return '+7 ('.substr($d10,0,3).') '.substr($d10,3,3).'-'.substr($d10,6,2).'-'.substr($d10,8,2);
	}
	elseif(strlen($d)==7){
		return '+7 (812) '.substr($d,0,3).'-'.substr($d,3,2).'-'.substr($d,5,2);
	}
	else{
		return $phone;
	}
}
Categories: PHP Tags:
21 октября 2021 Нет комментариев

При:

$cities_get=file_get_contents(LOS_URL.'ajax.php?type=cities');

Ошибка:

<br />
<b>Warning</b>:  file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in <b>/var/www/site.ru/index.php</b> on line <b>241</b><br />
<br />
<b>Warning</b>:  file_get_contents(): Failed to enable crypto in <b>/var/www/site.ru/index.php</b> on line <b>241</b><br />
<br />
<b>Warning</b>:  file_get_contents(https://site.su/ajax.php?type=cities): failed to open stream: operation failed in <b>/var/www/site.ru/index.php</b> on line <b>241</b><br />

Решение:

$arrContextOptions=array(
	"ssl"=>array(
		"verify_peer"=>false,
		"verify_peer_name"=>false,
	),
);
$cities_get=file_get_contents(LOS_URL.'ajax.php?type=cities',false,stream_context_create($arrContextOptions));
Categories: PHP Tags:
6 августа 2021 Нет комментариев

При ошибке

Access to XMLHttpRequest at 'https://site1.ru/api.php' from origin 'https://site2.ru' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

добавить на отдающий данные сайт (в примере site1.ru) заголовок

header('Access-Control-Allow-Origin: *');
Categories: PHP Tags: ,
3 августа 2021 Нет комментариев

Массив:

print_r($result_price);
Array
(
    [econom_mag] => 1800
    [econom] => 510
    [def] => 870
)

Ищем ключ минимального значения массива:

$min_key=array_keys($result_price,min($result_price));
print_r($min_key);
Array
(
    [0] => econom
)
Categories: PHP Tags: