douwen1901 2014-12-01 02:23
浏览 140
已采纳

如何使用PHP-CLI提示输入并将输入作为变量提供?

I am looking to make a small PHP-CLI tool that will tell me if a date that I input, is a weekend day. I currently have the following script, which achieves some of this goal:

<?php
function isWeekend($date) {
return (date('N', strtotime($date)) >= 6);
}
var_dump(isWeekend('2014-10-29')); 
?>

It currently works well (outputs true or false), but I must update the source each time I want to test a date. It would be easier if I could prompt for date (user input), and then use that input to calculate the date, preferably without using HTML elements, as I am currently running the script via the CLI.

Has anyone else managed to achieve what I was looking to do? I don't need validation, as its a tool that only I will use, and I will be sure to use the correct date structure each time I use it.

  • 写回答

2条回答 默认 最新

  • doumao8355 2014-12-01 02:39
    关注

    You can try with:

    <?php
    fwrite(STDOUT, "Enter the date
    ");
    $date = fgets(STDIN);      
    $isWeekend = (date('N', strtotime($date)) >= 6);
    fwrite(STDOUT, "Response: $isWeekend");  
    exit(0); 
    ?>
    

    This is what i am getting:

    C:\xampp\php>php test.php
    Enter the date
    2014-11-30
    Response: 1
    C:\xampp\php>php test.php
    Enter the date
    2014-12-01
    Response:
    C:\xampp\php>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了