this content was originally posted here.

How Many Days Since a Date and other Calculations

Here i calculate the number of days since a certain date in php and use the values for checking how much something costs per day. 

Start Date

Lets add the date that the item was purchaced. In date format Year-Month-Day

$date_start = date_create("2017-10-29");

Today's Date

Get todays date according to the server date with php

$todays_date = date_create(date('Y-m-d'));

Check the difference between the dates

Using the date_diff function we can check the difference between these dates.

$diff = date_diff($date_start,$todays_date);

Output the details

I usually like to add all the data into a variable and then return it so here we will add all the data to the variable $template_result. This will give us the amount of days since the original date.

$template_result = 'Number of Days Since 29th October 2017 is '.$diff->format("%a");

More Calculations

Using this number we can calculate different things like how many years or months since a date.

Years Since Calculation

Calculate the Years since the date.

$years_calc = $diff->format("%a") / 365;
$years_calc = number_format($years_calc, 2);
$template_result .= "<p>Years $years_calc </p>";

Cost per Day

If the item has an original value we can then calculate the cost per day using the original number.

$cost = 331;
$per_day = $cost / $diff->format("%a");
$per_day = number_format($per_day, 2);
$template_result .= "<p>Original Total Cost: $$cost, Cost per Day $$per_day </p>";

Return the results

Now we can just return the results or just echo them. 

echo $template_result;

You can see a working demo below. 

Demo

Number of Days Since 29th October 2017 is 543

Years 1.49

Original Total Cost: $331, Cost per Day $0.61

database file: /home/kruxor/Dropbox/ssl.kruxor.com/root/kruxor.com/db/db.db
comments_dbfile: /home/kruxor/Dropbox/ssl.kruxor.com/root/kruxor.com/db/comments.db ($db_comments)
views_dbfile: /home/kruxor/Dropbox/ssl.kruxor.com/root/kruxor.com/db/views.db
$dbfood_file: /home/kruxor/Dropbox/ssl.kruxor.com/root/kruxor.com/db/food.db
db_linx: /home/kruxor/Dropbox/ssl.kruxor.com/root/kruxor.com/linx/db/db.db
Classes Loaded:
class/answers.class.php
class/code.class.php
class/comments.class.php
class/content.class.php
class/core.class.php
class/crud.class.php
class/crumbs.class.php
class/featured.class.php
class/food.class.php
class/functions.class.php
class/images.class.php
class/import.class.php
class/links.class.php
class/questions.class.php
class/template.class.php
class/tv.class.php
class/user.class.php
class/utilities.class.php
class/views.class.php
class/widget.class.php
Class Extend Loaded: class/extends/expenses.extend.php
Class Extend Loaded: class/extends/gary_cummins_timeline.extend.php
Class Extend Loaded: class/extends/moo.extend.php
Class Extend Loaded: class/extends/osrs_item_data.extend.php
Class Extend Loaded: class/extends/osrs_slayer.extend.php
Class Extend Loaded: class/extends/osrs_slayer_monsters.extend.php
Class Extend Loaded: class/extends/searches.extend.php
Core Database Connected: expenses.db
expenses add_to_menu:
Class Extend Auto Initialized as: expenses
Core Database Connected: gary_cummins_timeline.db
gary_cummins_timeline add_to_menu:
Class Extend Auto Initialized as: gary_cummins_timeline
Core Database Connected: moo.db
moo add_to_menu:
Class Extend Auto Initialized as: moo
Core Database Connected: osrs_item_data.db
osrs_item_data add_to_menu:
Class Extend Auto Initialized as: osrs_item_data
Core Database Connected: osrs_slayer.db
osrs_slayer add_to_menu:
Class Extend Auto Initialized as: osrs_slayer
Core Database Connected: osrs_slayer_monsters.db
osrs_slayer_monsters add_to_menu:
Class Extend Auto Initialized as: osrs_slayer_monsters
Core Database Connected: searches.db
searches add_to_menu:
Class Extend Auto Initialized as: searches
$user_logged_in:
user access level:
pages:
$p1:view
$p2:code
$p3:emGp9
$p4:how-many-days-since-a-date-and-other-calculations
$p5:
$p6:
$p:/view/code/emGp9/how-many-days-since-a-date-and-other-calculations/
Using Template: view-code.html