Ryongyon!

这世界上假绅士太多,缺少训练有素的流氓

· 实践教程 · · 87548次浏览

Typecho实现类似微博几分钟、几天前的词义化时间

前言

事情是这样的,刚开始折腾Typecho的时候呢,想把评论框的时间显示成类似微博的 “几秒前” "几分钟前" "几天前"这样的词义化时间,但是却毫无头绪,于是到SegmentFault看到了这么一篇文章 《分钟前,小时前,天前,个月前,年前如何实现》,里面提供了好几种方案,但我不是很喜欢。突然我发现一个细节,在Typecho的后台,很多地方都是用这种 “XX前” 的词义化时间显示的,简单明了。从这里我们就可以得出,后台是有相关函数,我们只要找到,然后直接使用就可以了,So easy。于是我就开始翻后台文件,终于在 var/Typecho/I18n.php 找到了,代码如下:

/**
* 词义化时间
*
* @access public
* @param string $from 起始时间
* @param string $now 终止时间
* @return string
*/
public static function dateWord($from, $now)
{
$between = $now - $from;
/** 如果是一天 */
if ($between >= 0 && $between < 86400 && date('d', $from) == date('d', $now)) {
/** 如果是一小时 */
if ($between < 3600) {
/** 如果是一分钟 */
if ($between < 60) {
if (0 == $between) {
return _t('刚刚');
} else {
return str_replace('%d', $between, _n('一秒前', '%d秒前', $between));
}
}
$min = floor($between / 60);
return str_replace('%d', $min, _n('一分钟前', '%d分钟前', $min));
}
$hour = floor($between / 3600);
return str_replace('%d', $hour, _n('一小时前', '%d小时前', $hour));
}
/** 如果是昨天 */
if ($between > 0 && $between < 172800 
&& (date('z', $from) + 1 == date('z', $now) // 在同一年的情况 
|| date('z', $from) + 1 == date('L') + 365 + date('z', $now))) { // 跨年的情况
 return _t('昨天 %s', date('H:i', $from));
}
/** 如果是一个星期 */
if ($between > 0 && $between < 604800) {
$day = floor($between / 86400);
return str_replace('%d', $day, _n('一天前', '%d天前', $day));
}
/** 如果是 */
if (date('Y', $from) == date('Y', $now)) {
return date(_t('Y年n月j日'), $from);
}
return date(_t('Y年m月d日'), $from);
}

但是我发现官方提供的代码没有“周”和“月”,所以我动手完善了一下代码,添加了“周”和“月”,只有1年以及1年以上的时间才会显示完整时间。这样可能比较好点吧~ 可以自行考虑修改。完善后的代码如下:

/**
* 词义化时间
*
* @access public
* @param string $from 起始时间
* @param string $now 终止时间
* @return string
*/
public static function dateWord($from, $now)
{
$between = $now - $from;
/** 如果是一天 */
if ($between >= 0 && $between < 86400 && date('d', $from) == date('d', $now)) {
/** 如果是一小时 */
if ($between < 3600) {
/** 如果是一分钟 */
if ($between < 60) {
if (0 == $between) {
return _t('刚刚');
} else {
return str_replace('%d', $between, _n('刚刚', '%d秒前', $between));
}
}
$min = floor($between / 60);
return str_replace('%d', $min, _n('1分钟前', '%d分钟前', $min));
}
$hour = floor($between / 3600);
return str_replace('%d', $hour, _n('1小时前', '%d小时前', $hour));
}
/** 如果是昨天 */
if ($between > 0 && $between < 172800 
&& (date('z', $from) + 1 == date('z', $now)                             // 在同一年的情况 
|| date('z', $from) + 1 == date('L') + 365 + date('z', $now))) {    // 跨年的情况
return _t('昨天 %s', date('H:i', $from));
}
/** 如果是一个星期以内 */
if ($between > 0 && $between < 604800) {
$day = floor($between / 86400);
return str_replace('%d', $day, _n('1天前', '%d天前', $day));
}
/** 如果是一个星期以上 */
if ($between > 0 && $between < 2592000){
$week = floor($between / 648000);
return str_replace('%d', $week, _n('1周前', '%d周前', $week));
}
/** 如果是一年以内 */
if ($between > 0 && $between < 31557600){
$month = floor($between / 2629800);
return str_replace('%d', $month, _n('1个月前', '%d个月前', $month));
}
/** 如果是一年以上 */
if (date('Y', $from) == date('Y', $now)) {
return date(_t('Y年n月j日 H:i'), $from);
}
return date(_t('Y年m月d日 H:i'), $from);
}

食用方法

嗯...说了一堆废话,还是直接抛出食用方法吧。
编辑文件comments.php 找到以下代码:

<time class="comment-time"><?php $comments->date(); ?></time>

修改为:

<time class="comment-time"><?php $comments->dateWord(); ?></time>
评论 (22条)
  1. immisyday

    Zithromax

    回复
  2. chellobre

    priligy where to buy

    回复
  3. vokAlmowl

    plaquenil 200 mg

    回复
  4. Comment To Last Longer Naturellement

    回复
  5. Patients with both COVID-19 and destructive pleural mesothelioma showed high-pitched rates of hospitalization and mortality.

    Twenty percent of the patients with anti pleural mesothelioma at the Vall d’Hebron University Hospitals became infected with COVID-19 during the sooner year of the pandemic. This led to both a loved hospitalization and mortality stationary sound this people, showed findings from a descriptive swot that was presented during the 2021 Cosmos Congress on Lung Cancer.1

    The results showed that 7 patients (18%) were diagnosed with COVID-19 not later than a invalidate transcription polymerase liaison counterbalance prove; the median all-embracing survival (OS) of these patients was 17.8 months from cancer diagnosis.

    Five of these patients died; reasons an harmony to dying included COVID-19 (n = 4) and entire virus (n = 1). The median OS of these patients was 0.4 months since COVID-19 diagnosis.

    Patients with thoracic malignancies may be uncommonly feeble to COVID-19, vanguard ruminate on governor Susana Cedres, MD, PhD, a medical oncologist in the Thoracic Tumors Exposition at the Vall d’Hebron University Clinic and Newcomer of Oncology in Barcelona, Spain, said in explaining the infrastructure seeing that the measured once more in a accepted awarding of the data.

    Although the mastery of SARS-CoV-2 infection has in days gone auspices of been evaluated in 200 patients with thoracic malignancies in the TERAVOLT registry, limited 8 patients with malicious pleural mesothelioma were included in the assessment.

    In the Combined States, tuberculosis, stupefacient attain complex, hepatitis, HIV/AIDS, cardiomyopathy, diabetes, and toxic pleural mesothelioma has been associated with worse COVID-19 correlated mortality outcomes.

    So, investigators sought to torture unrestricted the shape of COVID-19 infection on patients with lethal pleural mesothelioma at Vall d’Hebron University Hospital.

    In the over, investigators compiled the medical records of 38 patients with life-threatening pleural mesothelioma who had visited Vall d’Hebron University Asylum between Lurch 2020 and Measure 2021.

    The materials that was controlled included communication on demographics, comorbidities, oncological nurture, and tow-path of COVID-19 illness.

    With assess to the constant characteristics of those diagnosed with COVID-19, the median maturation was 62 years (move away, 62-87). The post-mortem residents comprised 4 men (57%) and 4 women (43%). Four patients were non-smokers (57%), and 3 patients were acclaimed or one-time smokers (43%).

    All 7 patients (100%) had epithelioid histology. Five patients (71%) were not receiving oncologic treatment at COVID-19 diagnosis vs 2 (29%) patients who were.

    Additionally, investigators stratified patients during means of 6 characteristics at COVID-19 diagnosis: comorbidity, concomitant treatment, clinical onrush, laboratory, hospitalization, and respiratory symptoms.

    In the face of comorbidities, 4 patients (57%) had a cardiovascular comorbidity, 1 sufferer (14%) had a respiratory comorbidity, and 1 sufferer (14%) had a renal comorbidity.

    On the area of concomitant treatments, 4 patients (57%) were receiving antiplatelet or anticoagulant psychoanalysis, 2 patients (29%) were receiving antidiabetic treatment, and 1 compassionate (14%) was receiving corticosteroids.

    Apropos clinical onrush, 4 patients (57%) had symptomatic constituent, and 3 patients (43%) had asymptomatic onset.

    Pertaining to laboratory results, 6 patients (85%) had lymphopenia, 4 patients (57%) had patrician d-dimer, and 4 patients (57%) had soup‡on IL-6.

    Humbug anenst refer hospitalization, 6 patients (85%) were hospitalized, and 1 forbearing (14%) was not.

    On the course of read of respiratory symptoms, 4 patients (57%) had bilateral pneumonia, and 6 patients (85%) had oxygen support.

    Credentials

    Cedres S, Assaf JD, Iranzo P, et al. Clinical characteristics and outcomes in patients with bodily pleural mesothelioma (MPM) with COVID-19 infection. Presented at: 2021 Original Bull stage on Lung Cancer; September 8-14, 20

    https://te.legra.ph/mesothelioma-stage-4-06-26-2
    https://telegra.ph/how-is-mesothelioma-transmitted-06-26-2
    https://tgraph.io/mESOTHELIOMA-lIFE-eXPECTANCY-sTAGE-2-06-26-3
    https://telegra.ph/mesothelioma-pleural-effusion-stage-06-26-2
    https://te.legra.ph/how-do-you-get-checked-for-mesothelioma-06-26-2
    https://te.legra.ph/symptoms-of-mesothelioma-lung-cancer-06-26-2
    https://tgraph.io/pLEURAL-mESOTHELIOMA-sTAGE-4-lIFE-eXPECTANCY-06-26-4
    https://te.legra.ph/treatment-for-mesothelioma-cancer-06-26-4
    https://te.legra.ph/Information-About-Mesothelioma-06-26-2
    https://issuu.com/savoeunfqjqc/docs/fast_money_loans_for_students
    https://telegra.ph/survival-rate-of-mesothelioma-cancer-stage-4-06-26-2
    https://te.legra.ph/mesothelioma-cancer-treatment-centers-06-26-3
    https://te.legra.ph/Is-Mesothelioma-A-Form-Of-Lung-Cancer-06-26-3
    https://telegra.ph/Can-You-Survive-Malignant-Mesothelioma-06-26-2
    https://te.legra.ph/blood-test-for-mesothelioma-06-26-4
    https://telegra.ph/mesothelioma-stage-3-progression-06-26
    https://te.legra.ph/mesothelioma-symptoms-rales-06-26-6
    https://te.legra.ph/ADVANCED-MESOTHELIOMA-LIFE-EXPECTANCY-06-26-2
    https://te.legra.ph/How-Deadly-Is-Mesothelioma-06-26-2
    https://issuu.com/jasonnwto/docs/fast_hard_money_loans
    https://tgraph.io/BENIGN-MESOTHELIOMA-ABDOMEN-06-26-2
    https://tgraph.io/mESOthELiOMA-frOM-AsBEsTos-ExPosuRE-06-26-7
    https://tgraph.io/MesothELioma-pRogrEssioN-sYmPTOms-06-26-3
    https://te.legra.ph/sUrviVaL-RATE-oF-MeSOtheLIOmA-cAnceR-06-26-4
    https://te.legra.ph/5-year-survival-rate-mesothelioma-06-26-4
    https://te.legra.ph/trEAtMEnt-for-mesOTHELIoma-Cancer-06-26-4
    https://telegra.ph/RISK-OF-MESOTHELIOMA-AFTER-ASBESTOS-EXPOSURE-06-26-3
    https://telegra.ph/ePITHELIAL-mALIGNANT-mESOTHELIOMA-06-26-4
    https://telegra.ph/iS-mESOTHELIOMA-bENIGN-06-26-3
    https://issuu.com/savoeunfqjqc/docs/get_money_fast_loans
    https://te.legra.ph/is-mesothelioma-always-cancer-06-26-2
    https://telegra.ph/mesothelioma-immunostains-pathology-06-26-3
    https://te.legra.ph/How-To-Treat-Mesothelioma-Naturally-06-26-2
    https://telegra.ph/WHY-IS-MESOTHELIOMA-SO-DEADLY-06-26-2
    https://telegra.ph/How-Can-You-Prevent-Mesothelioma-06-26-2
    https://telegra.ph/hoW-meSOThELIOMa-deveLoPS-06-26-3
    https://telegra.ph/symptoms-of-mesothelioma-in-the-stomach-06-26-4
    https://telegra.ph/PrEvEnTIOn-of-MESoTHeLIoMA-06-26-5
    https://te.legra.ph/MaLIGNaNT-mesoTHelIoMA-sYmpToMs-06-26-4
    https://issuu.com/jasonnwto/docs/fast_money_loans_for_students
    https://tgraph.io/iS-mESOTHELIOMA-cANCER-cURABLE-06-26-3
    https://te.legra.ph/benign-cystic-mesothelioma-pathology-outlines-06-26-4
    https://telegra.ph/mesothelioma-ihc-pathology-06-26-3
    https://te.legra.ph/SYMPTOMS-OF-LATE-STAGE-MESOTHELIOMA-06-26-2
    https://telegra.ph/RISK-OF-MESOTHELIOMA-AFTER-ASBESTOS-EXPOSURE-06-26-3
    https://te.legra.ph/stage-4-mesothelioma-survival-06-26-3
    https://telegra.ph/how-to-diagnose-peritoneal-mesothelioma-06-26-2
    https://tgraph.io/Mesothelioma-Cancer-Asbestos-06-26-2
    https://te.legra.ph/pleural-mesothelioma-end-stages-06-26-4
    https://issuu.com/jasonnwto/docs/fast_money_car_title_loans
    https://tgraph.io/DiAGnOSTiC-TeST-FOR-MeSOthELIomA-06-26-4
    https://tgraph.io/average-life-expectancy-with-mesothelioma-06-26-4
    https://te.legra.ph/benign-cystic-mesothelioma-of-the-peritoneum-06-26-3
    https://tgraph.io/mESOTHELIOMA-aTTORNEY-hOUSTON-bULLOCK-lAW-06-26-3
    https://tgraph.io/how-long-does-a-mesothelioma-lawsuit-take-06-26-3
    https://telegra.ph/HoW-fAST-dOes-MesOTHeLiOMA-SpREad-06-26-3
    https://tgraph.io/how-long-does-it-take-mesothelioma-to-develop-06-26-3
    https://tgraph.io/does-mesothelioma-have-stages-06-26-3
    https://telegra.ph/asbestos-exposure-mesothelioma-06-26-5
    https://issuu.com/jasonnwto/docs/fast_cash_money_loans
    https://tgraph.io/lung-cancer-mesothelioma-symptoms-06-26
    https://te.legra.ph/how-do-you-know-if-you-have-mesothelioma-06-26-3
    https://tgraph.io/why-is-mesothelioma-so-deadly-06-26-2
    https://tgraph.io/hOW-dO-yOU-tREAT-mESOTHELIOMA-06-26-3
    https://telegra.ph/How-To-Test-For-Mesothelioma-06-26-4
    https://telegra.ph/Symptoms-For-Mesothelioma-06-26-2
    https://tgraph.io/malignant-epithelioid-mesothelioma-treatments-06-26
    https://telegra.ph/can-mesothelioma-be-inherited-06-26-3
    https://telegra.ph/best-lawyers-for-mesothelioma-06-26-2
    https://telegra.ph/symptoms-of-pleural-mesothelioma-06-26-3
    https://te.legra.ph/cancer-ribbon-color-for-mesothelioma-06-26-4
    https://issuu.com/jasonnwto/docs/get_money_fast_loans
    https://te.legra.ph/SYMPTOMS-OF-MESOTHELIOMA-LUNG-CANCER-06-26-2
    https://te.legra.ph/Abdominal-Mesothelioma-Symptoms-06-26-2
    https://telegra.ph/mesothelioma-cancer-alliance-06-26-2
    https://te.legra.ph/how-to-say-mesothelioma-06-26-2
    https://tgraph.io/mesothelioma-symptoms-rales-06-26-7
    https://te.legra.ph/wHAT-iS-tHE-sYMPTOMS-oF-mESOTHELIOMA-06-26-3
    https://te.legra.ph/end-stage-mesothelioma-what-a-patient-feels-06-26-4
    https://telegra.ph/Mesothelioma-How-Long-Does-A-Case-Take-06-26-4
    https://issuu.com/jasonnwto/docs/fast_money_loans_personal_loans
    https://te.legra.ph/MESOTHELIOMA-LUNG-CANCER-SYMPTOMS-06-26-3
    https://te.legra.ph/lab-test-for-mesothelioma-06-26-4
    https://telegra.ph/HAS-ANYONE-EVER-SURVIVED-MESOTHELIOMA-06-26-2
    https://te.legra.ph/is-mesothelioma-nonmalignant-06-26-2
    https://te.legra.ph/mESOTHELIOMA-sURVIVAL-rATE-sTAGE-1-06-26-4
    https://telegra.ph/is-mesothelioma-caused-by-smoking-06-26-2
    https://te.legra.ph/Is-mESOtHelIoMa-TrEAtABlE-06-26-6
    https://issuu.com/adamhyho/docs/fast_money_loan_3451_cherry_ave_long_beach_ca
    https://tgraph.io/mesothelioma-pathology-06-26-2
    https://telegra.ph/mesothelioma-metastasis-bone-06-26-4
    https://tgraph.io/how-do-you-get-checked-for-mesothelioma-06-26-3
    https://tgraph.io/cystic-mesothelioma-pathology-06-26-3
    https://te.legra.ph/hOW-dOES-mESOTHELIOMA-dEVELOP-06-26-5
    https://tgraph.io/Peritoneal-Mesothelioma-Stage-3-06-26-4
    https://tgraph.io/mesothelioma-and-asbestos-exposure-06-26-4
    https://telegra.ph/how-can-you-prevent-mesothelioma-06-26-2
    https://telegra.ph/what-causes-malignant-mesothelioma-06-26-4
    https://issuu.com/adamhyho/docs/get_money_fast_no_loans
    https://telegra.ph/mesothelioma-lawyers-houston-texas-06-26-3

    回复
  6. affelaync

    http://buyzithromaxinf.com/ - Zithromax

    回复
  7. Bundeby

    https://buystromectolon.com/ - where can i buy stromectol ivermectin

    回复
  8. Venangefs

    https://buytadalafshop.com/ - Cialis

    回复
  9. Pfizer Viagra Without Prescription

    回复
  10. Kamagra Ungesund

    回复