Tag Archives: php

Lấy số ngày trong một tháng

Về lý thuyết thì: Chỉ có số ngày trong tháng 2 là thay đổi theo năm. Còn lại đều không đổi.
– Mảng 12 phần tử ứng với số ngày của 12 tháng, mặc định tháng 2 coi như 28 ngày.
– Đang tính số ngày trong tháng mấy? Có phải tháng 2 không
+ Không => Xong, tra theo phần tử tương ứng trong mảng
+ Có: – Năm nhuận không? + Có thì trả về 29 + Không thì trả về 28

Tháng 1 – 31 ngày
Tháng 2 – ? ngày
Tháng 3 – 31 ngày
Tháng 4 – 30 ngày

Tháng 5 – 31 ngày
Tháng 6 – 30 ngày
Tháng 7 – 31 ngày
Tháng 8 – 31 ngày

Tháng 9 – 30 ngày
Tháng 10 – 31 ngày
Tháng 11 – 30 ngày
Tháng 12 – 31 ngày

Năm nhuận là năm: (Chia hết cho 400) hoặc (chia hết cho 4 nhưng ko chia hết cho 100)

Về sử dụng, sao ko tận dụng hàm có sẵn nhỉ?
Javascript:
return new Date(2010, 2, 0).getDate();

PHP:
return cal_days_in_month(CAL_GREGORIAN, 2, 2010);

How to get current function name

PHP
function myFunction($x, $y) {
$functionName = __FUNCTION__;
if ($functionName != ‘myFunction’) {
echo(‘Oap. Who am I?’);
} else {
echo ‘I love myself’;
}
}

Javascript
function someFunc() {
var ownName = arguments.callee.toString();
ownName = ownName.substr(‘function ‘.length); // trim off “function ”
ownName = ownName.substr(0, ownName.indexOf(‘(‘)); // trim off everything after the function name
alert(ownName);
}

Auto upload form by cURL and PHP

<?php
    $actionUrl = 'http://server/upload/jup.jsp';
    $post_params['title'] = 'How to upload auto by cURL and PHP';
    $post_params['tag'] = 'curl, php, auto, upload, submit, form, http, post';
    
    # File will be upload
    # First
    $post_params['ufile'] = '@/home/huypv/private/xxx.txt;text/plain';
    /*
——WebXXXFormBoundaryjmXSvBkBa2nO7Z3j
Content-Disposition: form-data; name="ufile"; filename="xxx.txt"
Content-Type: text/plain

CONTENT OF FILE xxx.txt
    */
    
    # And more
    $post_params['ifile'] = '@/home/huypv/private/yyy';
/*
——WebXXXFormBoundaryjmXSvBkBa2nO7Z3j
Content-Disposition: form-data; name="ifile"; filename="yyy"
Content-Type: application/octet-stream

CONTENT OF FILE yyy
*/

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $actionUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    #curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
    
    $result = curl_exec($ch);
    #var_dump( curl_getinfo($ch, CURLINFO_HEADER_OUT));
    curl_close($ch);

Server
<form action="http://server/upload/jup.jsp" method="post" enctype="multipart/form-data">
<input type="text" name="title" /><br />
<input type="text" name="tag" /><br />

<input type="file" name="ufile" /><br />
<input type="file" name="ifile" /><br />

<input type="submit" value="Post" />
</form>