Jump to content

Recommended Posts

hi there trying to create a very simple date counter script here.

I just cant recall how to do all this.

There is a span of days starting at oct 30th, 2013 and I want to get todays date and then determine how many days have passed.

Am I even on the right track here? Any help would be mmuch appreciated!

 

my feeble attempt is below:

 

<?php 

$elvenportal = mktime(0,0,0,10,30,2011);

$now = mktime(date("H"),date("i"),date("s"),date("n"),date("j"),date("Y"));

$theeday = date("d")-1;

$late = date("H");

if ($late < 7) {$now = mktime(0,0,0,date("n"),$theeday,date("Y"));};

if ($_POST["dayz"] != "" ){

if ($_POST["month"] != "" ){

if ($_POST["year"] != "" ){

$now = mktime(0,0,0,$_POST["month"],$_POST["dayz"],$_POST["year"]);

};};};

$offset = $now - $elvenportal;

print $offset;

?>

Link to comment
https://forums.phpfreaks.com/topic/283909-creating-a-simple-date-counter/
Share on other sites

try this method

$adate = new DateTime('2013-10-20');
$bdate = new DateTime('2013-11-01');
$now = new DateTime();

$period = new DatePeriod($adate, new DateInterval('P1D'), $bdate);

printf("%-15s\t%s\n", 'Date', 'Days elapsed');
foreach ($period as $dt) {
    printf("%-15s\t%6d\n", $dt->format('d M Y'), $dt->diff($now)->days);
}

/* RESULTS   ***************************************
Date               Days elapsed
20 Oct 2013            26
21 Oct 2013            25
22 Oct 2013            24
23 Oct 2013            23
24 Oct 2013            22
25 Oct 2013            21
26 Oct 2013            20
27 Oct 2013            19
28 Oct 2013            18
29 Oct 2013            17
30 Oct 2013            16
31 Oct 2013            15
*****************************************************/

I tried this and got the following error: Fatal error: Class 'DatePeriod' not found

 

I guess maybe this is from a newer version of php i'm working with?

What is the purpose of $bdate? I'm unclear on that.

 

Thanks so much for your help its much appreciated!

This should work with just DateTime objects

$adate = new DateTime('2013-10-20');
$bdate = new DateTime('2013-11-01');
$now = new DateTime();

#$period = new DatePeriod($adate, new DateInterval('P1D'), $bdate);
echo '<pre>';
printf("%-15s\t%s\n", 'Date', 'Days elapsed');
while ($adate < $bdate) {
    printf("%-15s\t%6d\n", $adate->format('d M Y'), 
        floor(($now->getTimestamp()-$adate->getTimestamp())/86400));
    $adate->modify('+1 days');
}
echo '</pre>';

I wonder why they introduced DateTime in 5.2 when you can't do anything with it until 5.3

 

Back to the old-fashioned ways

$adate = strtotime('2013-10-20');
$bdate = strtotime('2013-11-01');
$now = mktime(0,0,0);

echo '<pre>';
printf("%-15s\t%s\n", 'Date', 'Days elapsed');
while ($adate < $bdate) {
    printf("%-15s\t%6d\n", date('d M Y', $adate), floor(($now-$adate)/86400) );
    $adate = strtotime('+1 days', $adate);
}
echo '</pre>';

nice thats working, i used it like so:
 

$adate = strtotime('2013-10-30');
$bdate = strtotime('2013-10-31');
$now = mktime(0,0,0);
 
echo '<pre>';
printf("%-15s\t%s\n", ' ', ' ');
while ($adate < $bdate) {
    printf("%-15s\t%6d\n", 'this be how many days passed:<span>', floor(($now-$adate)/86400), '<span>' );
    $adate = strtotime('+1 days', $adate);
}
echo '</pre>';

now not exactly sure how to get this  number to print to a variable - i tried this and didnot succeed:
$howmanydayspassed =  printf("%-15s\t%6d\n", floor(($now-$adate)/86400) );

Really appreciate your help there you are awesome!
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.