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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题