Jump to content

Date differences


Steve_NI

Recommended Posts

I am trying to work the date difference between two dates

I have a form that a user completes and in that form they provide an end date.

In my php I convert this to a unix date:

$stamp = strtotime($_POST['endldate']);
$endDate = date('Y-m-d', $stamp);

 

I then want todays date:

$today = date('Y-m-d');

 

I then want to get the difference in days between the two.

I have tried $difference = datediff($endDate,$today)

 

But this returns an error = date_diff() expects parameter 1 to be DateTime

 

 

What i want to do is then divide the difference by 7 to get the number of weeks

 

How do I get around the error?

Link to comment
Share on other sites

You need to initialise your dates with the DateTime object before you use date_diff()

 

See examples here

http://www.php.net/manual/en/datetime.diff.php

 I followed the examples with my code:

$start = date_create($goalDate);
$end = date_create($today);
$interval = date_diff($goalDate,$today);

I still get the same error as before : date_diff() expects parameter 1 to be DateTime

The examples seem to use hard coded dates, but mine will depend on what the user inserts and also will differ dependent upon the current date.

Link to comment
Share on other sites

$goalDate is the string..  $start is the DateTime object

 

So    

 

$interval = date_diff($start,$end);

Thanks for your help

When I try:

$today = date('Y-m-d');
$start = date_create($goalDate);

$interval = date_diff($start,$today);

I get the error : date_diff() expects parameter 2 to be DateTime, string given

 

But when I then make the second parameter a string:

$start = date_create($goalDate);
$end = date_create($today);

$interval = date_diff($start,$end);

I get the error: Object of class DateInterval could not be converted to string

 

I'm getting myself very confused all I want is a number of days between the two dates that I could divide by 7 to work out the number of weeks between the them :confused:

Link to comment
Share on other sites


$objToday = new DateTime(date('Y-m-d'));  #  create datetime object for today
$objStart = new DateTime($goalDate);          # create datetime object for goal date

$objDuration = $objStart->diff($objToday, true);     # create a DateInterval object using the diff method of our $objStart datetime object passing in $objToday.

echo 'Days since start date: ' . $objDuration->format('d');    #  call the format method of our DateInterval object to say we just want the number of FULL (whole) DAYS  since the start
Link to comment
Share on other sites

For the interval format you need format('%a')

 

eg

$goalDate = '2013-06-20';
$objToday = new DateTime();
$objStart = new DateTime($goalDate);

$objDuration = $objStart->diff($objToday, true);

echo 'Days since start date: ' . $objDuration->format('d');   // --> d
echo 'Days since start date: ' . $objDuration->format('%d');  // --> 4  (days remaining after months calculated)
echo 'Days since start date: ' . $objDuration->format('%a');  // --> 157  (total days)
// or
echo 'Days since start date: ' . $objDuration->days;  // --> 157
Edited by Barand
Link to comment
Share on other sites

$objToday = new DateTime(date('Y-m-d'));  #  create datetime object for today
$objStart = new DateTime($goalDate);          # create datetime object for goal date

$objDuration = $objStart->diff($objToday, true);     # create a DateInterval object using the diff method of our $objStart datetime object passing in $objToday.

echo 'Days since start date: ' . $objDuration->format('d');    #  call the format method of our DateInterval object to say we just want the number of FULL (whole) DAYS  since the start

Appreciate your efforts I tried that but it printed out Days since start date d !

Link to comment
Share on other sites

ooops  change   

 

 

For the interval format you need format('%a')

 

eg

$goalDate = '2013-06-20';
$objToday = new DateTime();
$objStart = new DateTime($goalDate);

$objDuration = $objStart->diff($objToday, true);

echo 'Days since start date: ' . $objDuration->format('d');   // --> d
echo 'Days since start date: ' . $objDuration->format('%d');  // --> 4  (days remaining after months calculated)
echo 'Days since start date: ' . $objDuration->format('%a');  // --> 157  (total days)
// or
echo 'Days since start date: ' . $objDuration->days;  // --> 157

 

Thanks   :thumb-up:   Use %a , I mean use  $objDuration->days;   lol

Edited by objnoob
Link to comment
Share on other sites

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.