devwiki:php

Differences

This shows you the differences between two versions of the page.


Previous revision
devwiki:php [2022/11/16 16:16] (current) – [Python to Php] ying
Line 1: Line 1:
 +====== PHP and SQL ======
 +
 +  * select today data entry <code>where DATE(`time_entry`) = CURDATE()</code>
 +  * ref: date related: https://popsql.com/learn-sql/mysql/how-to-query-date-and-time-in-mysql
 +  * select current week data entry with option of week start day <code>
 +where YEARWEEK(`time_entry`) = YEARWEEK(CURDATE())
 +where YEARWEEK(`time_entry`, 1) = YEARWEEK(CURDATE(), 1)</code>
 +    * Note: weekofyear return week number, means previous year will return too <code>where  WEEKOFYEAR(`time_entry`) = WEEKOFYEAR(NOW());</code>
 +  * select current month data entry <code>where YEAR(`time_entry`) = YEAR(NOW()) AND MONTH(`time_entry`)=MONTH(NOW());</code>
 +  * select past 24 hr <code>where `time_entry`> DATE_SUB(NOW(), INTERVAL 1 DAY)</code>
 +  * select past 7 days <code>where `time_entry`> DATE_SUB(NOW(), INTERVAL 1 WEEK)</code>
 +  * select past 1 month time <code>where `time_entry`> DATE_SUB(NOW(), INTERVAL 1 MONTH)</code>
 +
 +
 +  * array related <code>
 +// sort array
 +sort($my_list);
 +</code>
 +
 +  * unicode data insert, make sure
 +    - database table's charset is utf8mb4 in sql<code>
 +CREATE TABLE info_table (
 +  id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
 +  name varchar(50) NOT NULL,
 +  edit_time DATETIME NOT NULL
 +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 +</code>
 +    - make sure database connect is set to utf8mb4 mode in php before query and insert<code>
 +mysqli_set_charset($con, "utf8mb4");
 +</code>
 +    - make sure charset is declared in html <code>
 +<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 +</code>
 +====== Php Date and Time Related ======
 +  * all time info <code>
 +date('N'); // current day of week, 6 for saturday
 +date('N', date_val) // day of week for that date data
 +date("Y-m-d"); // today's date string
 +</code>
 +
 +  * get current week range (monday to sunday) or any day's week range <code php>
 +//ref: https://stackoverflow.com/questions/5552862/finding-date-range-for-current-week-month-and-year
 +function rangeWeek($datestr) {
 +    date_default_timezone_set('Asia/Singapore');
 +    $date_val = strtotime($datestr);
 +    return array (
 +     "start" => date('N', $date_val) == 1 ? date ('Y-m-d', $date_val) : date ('Y-m-d', strtotime('last monday', $date_val)),
 +     "end" => date('N', $date_val) == 7 ? date ('Y-m-d', $date_val) : date ('Y-m-d', strtotime('next sunday', $date_val))
 +    );
 +}
 +echo rangeWeek('2011-4-5');
 +</code>
 +
 +  * show during time range: https://stackoverflow.com/questions/25921296/i-would-like-to-show-messages-according-to-time-range-between-two-time-stamps/25921549
 +
 +====== Python to Php ======
 +
 +^   ^ python ^ php ^
 +| string operation | word.strip() | trim($word) |
 +|  | word[1:5] | substr($word, 1, 5) |
 +|  | word[1:] | substr($word, 1) |
 +|  | prefix+subfix | $prefix.$subfix |
 +|  | text.replace('find','replace') | str_replace('find','replace',$text) |
 +|  | text.lower() | strtolower($text) |
 +|  | text.startswith(prefix) | ? |
 +| list operation | res_list = [x.strip() for x in txt.split('\n')] | $res_list = array_map('trim', explod('\n', $txt)) |
 +| | | <code>$res_list = array_map('trim', preg_split("/\r\n|\n|\r/", $selected) ); // ok for text</code> |
 +| | res_list.append(tmp_img) | array_push($res_list, $tmp_img); |
 +| |  | $res_list[] = $tmp_img; |
 +| | for each in word_list: | foreach($word_list as $each){} |
 +| | if id in check_list: | if(in_array($id, $check_list)){} |
 +| logic | if mark == '#': pass; elif mark == '*' | if($mark==="#"){} elseif($mark==="*"){} |
 +
 +<code>
 +php: 
 +$tmp_path = $path.strtolower(str_replace(' ','_', trim(substr($each,1)))).'.jpg';
 +$tmp_img = sprintf('<br><img src="%s">', $tmp_path);
 +array_push($res_list, $tmp_img);
 +join("\n",$res_list);
 +
 +python: (human logic writing)
 +tmp_path = path + each[1:].strip().replace(' ','_').lower()+ '.jpg'
 +tmp_img = '<br><img src="{0}">'.format(tmp_path)
 +res_list.append(tmp_img)
 +'\n'.join(res_list)
 +</code>
 +
 +====== Use PHP as HTML File output process ======
 +
 +  * other than serve php page through apache server, php in commandline also can be used to create html local page.
 +  * cmd code for render php to html file<code>
 +cd php_root_path
 +php.exe D:\path_to_php_web_page\test.php D:\path_to_php_web_page\test_out.html
 +</code>