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 8386721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:01:14+00:00 2026-06-09T18:01:14+00:00

So in PHP I have some code that extracts the day, month and year

  • 0

So in PHP I have some code that extracts the day, month and year as an integer with something like this:

// Grab the Date
    $date = date("mdy");
    $day = (int) substr($date, 2, 2);
    $month = (int) substr($date, 0, 2);
    $yr = (int) substr($date, 4);

I’d like to do the same in Ruby. And I thought I’d found the answer with

# Grab the Date
    now = Date.new(Time.now).to_date
    date = Date.parse(now)
    day = date.mday
    month = date.mon
    yr = date.year

I’ve tried variations on this theme and every time it fails with

private method `to_date’ called for Tue Aug 14 01:16:00 -0600 2012:Time (NoMethodError)

I’m sure the answer is somewhere on the web but I’m not putting the right question to Google because I haven’t found it. I’ve only been coding in Ruby off and on for a few months and I imagine it is something simple. So what am I missing?

Thanks

  • 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-06-09T18:01:16+00:00Added an answer on June 9, 2026 at 6:01 pm

    I think you just want:

    now = Time.now
    
    day = now.mday
    
    month = now.mon
    
    yr = now.year
    

    By the way, you can do that more cleanly, without the substr() operations, in PHP:

    $date = new DateTime;
    
    $day = (int) $date->format( 'j' );
    
    $month = (int) $date->format( 'n' );
    
    $year = (int) $date->format( 'Y' );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP, say that you have some code like this: $infrastructure = mt_rand(0,100); if
I have some php code that was like this: $row = mysql_fetch_array ( mysql_query(SELECT
So I have some PHP code that looks like: $message = 'Here is the
I have some code like this: var mySelect = document.getElementById(mySelect); mySelect.innerHTML = <option>Loading...</option>; $.get(ajaxPage.php,
I have some code that appears to behave differently between PHP 4 and PHP 5. This
I have some PHP code that should cause and catch two exceptions: try{ @$this->connector->connect();
I have some PHP code that inserts a date into a table: INSERT INTO
I have some code that looks like this (I left out the parts that
I have some PHP code that generates dynamic tables of data on the fly.
I have some PHP code that is designed to make a spreadsheet with formulas

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.