PHP中的星期日至星期六的星期几数组

时间:2018-11-04 16:07:37

标签: php php-7 php-5.6

为了获取日历周的星期日至星期六,我正在php中使用该代码:

$dt = strtotime(date('Y-m-d'));

    $res['start'] = date('N', $dt)==1 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('last sunday', $dt));

    $res['end'] = date('N', $dt)==7 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('saturday', $dt));

    $day_of_week = date('N', strtotime($res['start']));


    $given_date = strtotime( $res['end'] );

    $first_of_week =  date('Y-m-d', strtotime("- {$day_of_week} day", $given_date));

    $first_of_week = strtotime($first_of_week);

    for($i=1 ;$i<=7; $i++)
    {
        $datess[] = date('Y-m-d', strtotime("+ {$i} day", $first_of_week));

        $week_array[] = date('m/d', strtotime("+ {$i} day", $first_of_week));
  print_r($datess);
    }
exit;

在此之后,我将从2018-10-29到2018-11-04获取数组,但是我想要从2018-11-04 2018-11-10数组,我在此代码中做错什么了,有人可以让我吗知道或纠正我我在此代码中做错的地方

2 个答案:

答案 0 :(得分:1)

您是否假设星期日是星期几1?而星期六7

如果是,则星期天为7,星期六为6。因此您的代码将如下所示:

$dt = strtotime(date('Y-m-d'));

$res['start'] = date('N', $dt) == 7 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('last sunday', $dt));

$res['end'] = date('N', $dt) == 6 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('saturday', $dt));

$day_of_week = date('N', strtotime($res['start']));


$given_date = strtotime($res['end']);

$first_of_week =  date('Y-m-d', strtotime("- {$day_of_week} day", $given_date));

$first_of_week = strtotime($first_of_week);

for ($i = 1 ; $i <= 7; $i++) {
    $datess[] = date('Y-m-d', strtotime("+ {$i} day", $first_of_week));

    $week_array[] = date('m/d', strtotime("+ {$i} day", $first_of_week));
    print_r($datess);
}

答案 1 :(得分:0)

使用PHP功能强大的日期函数time datestrtotime

的一种解决方案
<?php
use PHPMailer\PHPMailer\PHPMailer;
require_once "functions.php";

if (isset($_POST['email'])) {
    $conn = new mysqli('localhost', 'root', '', 'scheduling');

    $email = $conn->real_escape_string($_POST['email']);

    $sql = $conn->query("SELECT idnumber FROM user WHERE email='$email'");
    if ($sql->num_rows > 0) {

        $token = generateNewString();

        $conn->query("UPDATE user SET token='$token', 
                  tokenExpire=DATE_ADD(NOW(), INTERVAL 5 MINUTE)
                  WHERE email='$email'
        ");

        require_once ('PHPMailer/PHPMailer.php');
        require_once ('PHPMailer/Exception.php');
        require_once ('PHPMailer/SMTP.php');

        $mail = new PHPMailer();
        $mail->addAddress($email);
        $mail->setFrom("******@gmail.com", "*****");
        $mail->Subject = "Reset Password";
        $mail->isHTML(true);
        $mail->Body = "
            Hi,<br><br>

            In order to reset your password, 
please click on the link below:
<br>
            <a href='
            localhost/resetPassword.php?
email=$email&token=$token
            '>localhost/resetPassword.php? 
email=$email&token=$token</a><br><br>

            Kind Regards,<br>
            My Name
        ";

        if ($mail->send())
exit(json_encode(array("status" => 1, "msg" => 'Please Check Your Email 
Inbox!')));
        else
            exit(json_encode(array("status" => 0, "msg" => 'Something Wrong 
Just Happened! Please try again!')));
    } else
        exit(json_encode(array("status" => 0, "msg" => 'Please Check Your 
Inputs!')));
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, user-scalable=no, initial-scale=1.0, 
maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Forgot Password System</title>
<link rel="stylesheet" 
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" 
integrity="sha384- 
Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" 
crossorigin="anonymous">
</head>
<body>
<div class="container" style="margin-top: 100px;">
    <div class="row justify-content-center">
        <div class="col-md-6 col-md-offset-3" align="center">
            <input class="form-control" id="email" placeholder="Your Email 
Address"><br>
            <input type="button" class="btn btn-primary" value="Reset 
Password">
            <br><br>
            <p id="response"></p>
        </div>
    </div>
</div>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256- 
FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"> 
</script>
<script type="text/javascript">
    var email = $("#email");

    $(document).ready(function () {
        $('.btn-primary').on('click', function () {
            if (email.val() != "") {
                email.css('border', '1px solid green');

                $.ajax({
                   url: 'forgotPassword.php',
                   method: 'POST',
                   dataType: 'json',
                   data: {
                       email: email.val()
                   }, success: function (response) {
                        if (!response.success)
                            $("#response").html(response.msg).css('color', 
"red");
                        else
                            $("#response").html(response.msg).css('color', 
"green");
                    }
                });
            } else
                email.css('border', '1px solid red');
        });
    });
</script>
</body>
</html>