WordPress.org

Forums

How to add a simple number that shows how many days has passed? (3 posts)

  1. Uduse
    Member
    Posted 1 year ago #

    I was trying to display a single number that shows how many days our organization has been established.

    e.g. assume we started at 5/12/2014, and it will shows "2" today. If I visit it on tmr, 5/14/2014, it will show "3".

    can this be solved by a short piece of php or html?

    thanks.

  2. Dave Naylor
    Member
    Posted 1 year ago #

    Try this:

    $date_one = new DateTime("2014-05-12");
    $date_two = new DateTime("now");
    $daydiff = $date_one->diff($date_two);
    if ($daydiff->days == 1) {
        $my_day = " day.";
    } else {
        $my_day = " days.";
    }
    echo "We've been established for " . $daydiff->days . $my_day;
  3. Uduse
    Member
    Posted 1 year ago #

    Thanks a lot.

    Though I know nothing about php, here's my try:

    <html>
      <div align="center">
      We have been here for
      <br/>
      <br/>
      <br/>
      <?php
        date_default_timezone_set('America/Los_Angeles');
        $date_one = new DateTime("2014-03-05");
        $date_two = new DateTime("now");
        $daydiff = date_diff($date_one, $date_two);
        echo $daydiff->days;
      ?>
      <br/>
      <br/>
      days
      </div>
    </html>

    it works on online version of php ( like this one http://www.compileonline.com/execute_php_online.php )

    but it doesn't work on my blog.

    the display on my blog is like this:

    We have been here for 
    
    days; ?> 
    
    days

    any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.