Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6999363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:31:53+00:00 2026-05-27T20:31:53+00:00

I am trying to create a basic select menu using PHP variables for given

  • 0

I am trying to create a basic select menu using PHP variables for given dates. The date would select the 1st of the current month and subtract a given number of months to display according to other system rules. I am running into an error though and not sure why. I have broken the php out of my HTML completely just to test the class. Here are my findings:

Code

<?php 
    $date = new DateTime(date('Y').'-'.date('m').'-01'); 
    $date_now = $date->format('Y-m-d');
    $date->sub(new DateInterval('P12M'));
    $end_date = $date->format('Y-m-d');

    echo '1: '. $date_now. ' - '.strtotime($date_now).'<br />'; //works
    echo '2: '. $end_date. ' - '.strtotime($end_date).'<br />'; //works
    echo '3: '. $date_now = date("Y-m-d", strtotime("-1 month", strtotime($date_now))).'<br />'; //works
    echo '4: '. strtotime($date_now).'<br />'; //does not give any response
    echo '5: '. $date_now = date("Y-m-d", strtotime("-1 month", strtotime($date_now))).'<br />'; //does not work
    echo '6: '. $date_now; //does not work
    exit;
?>

Results

1: 2011-12-01 – 1322719200

2: 2010-12-01 – 1291183200

3: 2011-11-01

4:

5: 1969-12-01

6: 1969-12-01

As you can see – statement 4 does not echo at all and 5/6 are seemingly incorrect. Any ideas as to why this is occurring? Is it something to do with the way DateTime results are returned?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:31:54+00:00Added an answer on May 27, 2026 at 8:31 pm

    In your third echo you’re concatenating
    to the date string when you assign a new value to $date_now and this is causing strtotime() to fail on the next line.

    btw, you’re using the DateInterval class in the first few lines but not further down in the script. Is there any reason for that?

    Also, you should invest in a debugger (ZendDebugger or xDebug are the two most popular ones). I spotted this error in about 30 seconds flat using a debugger…

    EDIT: In reply to OP’s comment, you can still do the while statement using the DateTime class:

    while( $date_now >= $end_date ) {
       echo ...;
       $date_now->sub(new DateInterval('P1M'));
    }
    

    But you might need to make sure you clone the DateTime object when you create the end date as otherwise you’ll end up looping back from 1 year ago forever…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a very basic Setup and Deployment project using Visual Studio.
I'm trying to create a fairly simple macro in Word 2010 using Visual Basic.
I am using Visual Basic 2010 and am trying to create a datagrid where
I'm trying to create a basic macro recording/playback system. If I wanted to select
I am trying to create a user login/creation script in PHP and would like
I'm trying to create a basic system that will allow only specific users to
I'm trying to create a basic shopping cart, having an issue with the product
Question I'm trying to create a basic wxWidgets program with a text entry box,
I am trying to just create a basic layout, but i am having trouble
I'm trying to create an element in an XML where the basic content is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.