[英]PHP date() increase/decrease week store in $_SESSION
The following code will increase/decrease week by 1: (yes, my application requires the value to be stored in a $_SESSION
. 以下代码将逐周增加/减少1 :(是的,我的应用程序需要将值存储在$_SESSION
。
if (isset($_POST['decrease'])) {
$week_value = ($_SESSION['sess_week'] = $_SESSION['sess_week']-1);
}
else if (isset($_POST['increase'])) {
$week_value = ($_SESSION['sess_week'] = $_SESSION['sess_week']+1);
}
else {
$week_value = ($_SESSION['sess_week'] = date('W'));
}
echo $week_value;
However I would like it to reset at week 52 (new year). 不过我希望它能在第52周(新年)重置。 How can I do this? 我怎样才能做到这一点? I'm new to programming. 我是编程新手。
You can use the date("W")
function to get the week number of the current time. 您可以使用date("W")
功能获取当前时间的周数。 If it is week 1, simply set it to 1 (or 0 if you start there). 如果是第1周,只需将其设置为1(如果从那里开始则为0)。
You can play around and test it out by using a mktime()
as a second parameter in the date function to verify the outputs. 您可以使用mktime()
作为日期函数中的第二个参数来进行测试并验证输出。
<?php
if (date("W")==1)
{
$week_value =1;
}
else
{
if (isset($_POST['decrease'])) {
$week_value = ($_SESSION['sess_week'] = $_SESSION['sess_week']-1);
}
else if (isset($_POST['increase'])) {
$week_value = ($_SESSION['sess_week'] = $_SESSION['sess_week']+1);
}
else {
$week_value = ($_SESSION['sess_week'] = date('W'));
}
}
?>
if (isset($_POST['decrease'])) {
$week_value = $_SESSION['sess_week'] == 1 ? 52 : --$_SESSION['sess_week'];
}
else if (isset($_POST['increase'])) {
$week_value = $_SESSION['sess_week'] == 52 ? 1 : ++$_SESSION['sess_week'];
}
else {
$week_value = ($_SESSION['sess_week'] = date('W'));
}
One possibility to do this is as follows. 这样做的一种可能性如下。 (I took it that you meant that if the week is more than 52 it shall be 1. Additionally I added that if the week drops below 1 it shall be 52 again, thus the last week of the last year). (我认为你的意思是,如果一周超过52,它应该是1.另外我补充说,如果一周低于1,它将再次为52,因此是去年的最后一周)。
if (isset($_POST['decrease']))
{
$week_value=$_SESSION['sess_week']-1;
}
else
{
if (isset($_POST['increase']))
{
$week_value=$_SESSION['sess_week']+1;
}
else
{
$week_value=date('W');
}
}
if ($week_value>52)
{
$week_value=1;
}
else
{
if ($week_value<1)
$week_value=52;
}
$_SESSION['sess_week']=$week_value;
Do modulo ( %
) with 52. 用52进行模数( %
)。
That way you can assure that your week will always be less than 52. 这样你可以确保你的一周总是少于52周。
尝试这个:
if( $week_value == 52 )$week_value = 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.