简体   繁体   English

根据当前月份动态选择日期 - 选择列表

[英]Dynamic selection of dates based on current month - select list

I am trying to create, rather than using datepicker, an options list of the 1st days of the month (ie previous months 5, next months 2) 我试图创建,而不是使用datepicker,一个月的第一天(即前几个月5,下个月2)的选项列表

Given now is APRIL, this would be 01/04/2019 (dd/mm/yy) So the option list would span, December 2018, up till June 2019, of each 1st of the month. 现在是APRIL,这将是01/04/2019(年/月/日)所以选项列表将跨越2018年12月,直到2019年6月,每月1日。 Example would be as below, and then using a for loop to display the options 示例如下,然后使用for循环显示选项

var previous = mktime(0, , 0, date("m")-5  , date("d"), date("Y"));
var current = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
var next = mktime(0, 0, 0, date("m")+2  , date("d"), date("Y"));

Hope is it not to vague of a question, thank you for the help 希望不要模糊一个问题,谢谢你的帮助

It is straightforward in PHP using relative formats . 在PHP中使用相对格式是直截了当的。

An example is: 一个例子是:

$timenow = strtotime('first day of',time());

echo "\n";
for ($i = -5; $i < 3; $i ++) {
    $new = strtotime("$i month",$timenow);
    echo $new ," <-- ",strftime('%Y-%m-%d ',$new),"\n";
}

This uses 'first day of' 这使用'第一天'

'first day of' '第一天'
Sets the day of the first of the current month. 设置当前月份的第一天。 This phrase is best used together with a month name following it. 此短语最好与其后的月份名称一起使用。
"first day of January 2008" “2008年1月的第一天”

And ' number space unit' 和' 数字空间单位'

number space? 数字空间? (unit | 'week') (单位|'周')
Handles relative time items where the value is a number. 处理值为数字的相对时间项。
"+5 weeks", "12 day", "-7 weekdays" “+5周”,“12天”,“ - 7个工作日”

If you want to do it in javascript, suggest internet search for "js relative date". 如果你想在javascript中做,建议互联网搜索“js相对日期”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM