Question

I'm currently using the Gravity Forms plugin and it's set up to limit form entries per day, week, month and year; unfortunately, I need it by hour. Here is the original code that I'm editing:

private static function get_limit_period_dates($period){
    if(empty($period))
        return array("start_date" => null, "end_date" => null);

    switch($period){

        case "day" :
            return array(
                "start_date" => gmdate("Y-m-d"),
                "end_date" => gmdate("Y-m-d 23:59:59"));
        break;

        case "week" :
            return array(
                "start_date" => gmdate("Y-m-d", strtotime("Monday this week")),
                "end_date" => gmdate("Y-m-d 23:59:59", strtotime("next Sunday")));
        break;

        case "month" :
            $month_start = gmdate("Y-m-1");
            return array(
                "start_date" => $month_start,
                "end_date" => gmdate("Y-m-d H:i:s", strtotime("{$month_start} +1 month -1 second")));
        break;

        case "year" :
            return array(
                "start_date" => gmdate("Y-1-1"),
                "end_date" => gmdate("Y-12-31 23:59:59"));
        break;
    }
}

I've attempted the following code, I figured starting at 0 minutes and 0 seconds, then adding an hour would do the trick, but it isn't working:

case "hour" :
            return array(
                "start_date" => gmdate("H:00:00"),
                "end_date" => gmdate("H:i:s", strtotime("+1 hour")));
        break;

I've never used the gmdate function, so I'm eager to get a grasp on this if someone's willing to briefly explain what I'm missing. Thanks!

Was it helpful?

Solution

I think this might be what you want:

case "hour" :
    $hour_start = gmdate("Y-m-d H:00:00");
    return array(
        "start_date" => $hour_start,
        "end_date" => gmdate("Y-m-d H:i:s", strtotime("{$hour_start} +1 hour")));
break;

You have to do 1 hour after the start hour. Otherwise it takes it as 1 hour from now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top