I'm having a table as follows to store future date,
email date
abc#gmail.com 8/10/2014
and I want to do is to find the difference between the above date and the sever date.
I'm using date("m/d/Y") to get the current date.
If date("m/d/Y") = 07/20/2014, then I need the answer as 21.
Please help me find the difference between those days using PHP & MySQL, or suggest a better way to find the difference in days.
You can convert date to timestamp then calculate the days:
(int)(strtotime('8/10/2014')-strtotime('07/20/2014'))/60/60/24
The easiest way would be to store your dates as timestamps, then you could subtract the current timestamp with the one you've saved in a database.
The PHP function time() returns the current timestamp – that is the number of seconds since the 1st of January 1970. You can then format a timestamp $stamp to your liking with date('m/d/Y', $stamp), for example.
Aside from facilitating arithmetic operations with dates, you can display more or less information with timestamps by formatting them with date(), as well as show different formats (July 13, 2014 vs. 07/13/2014). If you save a date as a string, e.g. "8/10/2014", it will be very complicated for you to change the format, and it won't be possible to get the correct time, for example.
Finding how long ago in days a timestamp $stamp was to the current time is very easy:
$now = time();
$days = ($stamp - $now) / (24*3600);
Use round() to get a full number if desired (e.g. 7.2309 would simply become 7).
I'd personally do it using DateTime:
Select your date out into a variable. In this instance We'll call it $DBDate
$DBDateObj = DateTime::createFromFormat('j/m/Y', $DBDate);
$TodayDateObj = new DateTime();
$interval = $TodayDateObj->diff($DBDateObj);
$daysDiff = $interval->days;
In $daysDiff you should now have the difference in days.
Related
I am using PHP and mysql and using either Date or DateTime to save dates in mysql database. On site I have been displaying dates the way they are saved in database.
But now I want to show dates EVERYWHERE on site using one format:
April 17 2013
or
April 17 2013 12:20:50
I know I can use date and strtotime functions to display dates in above format. However there are a lot of places where I have date displaying code. So I am looking to automate the process where my current code works and displays dates in above format.
Any idea of how mysql trigger or some php magic could be created that converts all dates run through SELECT query automatically without changing my sql or php code since I have a lot of places in my code and it would be overkill to change code at all places?
For Example:
Date Saved in DB: 2013-04-16 12:41:26
SELECT QUERY: SELECT * FROM myTable
PHP: echo $row->dated; displays 2013-04-16 12:41:26
I want that without changing my php code, dates should be shown in above mentioned format globally on whole site.
Any ideas please how it could be achieved ?
You can directly format in via query using DATE_FORMAT()
SELECT DATE_FORMAT(myDate, '%M %d %Y %h:%i') myDate
FROM TableName
SQLFiddle Demo
and echo in your PHP: $row->myDate
MySQL Trigger doesn't project values and It is only fired during CrUD operations.
I would like to suggest you an alternative approach which i love to use.
You should use the epoch time. An epoch time is basicly the number of second that has passed since 1 January 1970
One if the benefits i love is that it is very easy to calculate
differences in time since you are just dealing with number of
seconds and not a complicated format such as sec min hrs
Another benefit is that it is very easy to store since its a
integer so you can store it in a sql db and have your php code understand it without worrying about the format and things like that.
In php, if you use the time() function, it will return the epoch time.
And if you ever want to display it in a user friendly way. you can use the following code:
$epoch = time();
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
As you can see, the format of the date is very flexible and thus easy to use.
A nice example to find the date 1 week ago:
$epoch = time() - 604800; //604800 seconds = 7 days
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
I have a date stored in a database with timezone UTC, the date is a few days/hours/minutes ahead of right now.
I then need to calculate the difference in time between right now and the given date, bearing in mind that there could be different timezones at play.
I have been doing the following but it is wrong as the timezone isn't taken into consideration.
$interval = date_create('now')->diff(date_create($listing_end_date));
//$interval->d = days
//$interval->h = hours
//$interval->i = minutes
How can I do the above with the timezone taken into account?
You need to pass a second argument to date_create telling it what time zone is represented by the date in $listing_end_date. For example, assuming the string is in UTC:
$interval = date_create('now')->diff(date_create($listing_end_date,
new DateTimeZone('UTC')));
How do I compare a time written in this format with the current time to see if this exact moment has happened yet (It's for a countdown timer), but the time must be in this format:
12/31/2012 5:00 AM UTC-0500
Ultimately I want to say if the current time is less than that date, display this, otherwise, display this.
strtotime can convert string dates to unix timestamp which you can easily use later for comparisons/etc
$timestamp = strtotime("12/31/2012 5:00 AM UTC-0500");
if (time() > $timestamp)
...
I wish to calculate the difference b/w 2 times in min:sec format . so is my approch correct
date("i:s",(strtotime($User['end_time']) - strtotime($User['start_time'])));
You may get the problems with timezones on some servers.
A bettter way would be using UTC timezone for calculation:
$date = new DateTime('', new DateTimeZone('UTC'));
$date->setTimestamp(strtotime($User['end_time']) - strtotime($User['start_time']));
echo $date->format('i:s');
Another thing, if they are different in exactly 1 hour, the result will be 00:00
strtotime($User['end_time']) - strtotime($User['start_time']) gives you the difference in seconds. Then you pass it to date, so you get the minute and second of the date whose unix timestamp is that.
I need to make a timestamp to put into MySQL. The user is submitting a number (of weeks) I need to add that many weeks to today's date. What I am trying to do is calculate an end date of an ad that the user is submitting.
Any suggestions on how to do this? Thanks!
You can use strtotime to add time to a date - it takes a second argument that is the current time if none is passed. You can then pass that created time to the date function to create your timestamp:
$timestamp = date('Y-m-d H:i:s', strtotime('+10 weeks'));
I think DATE_ADD(CURDATE(), INTERVAL 2 WEEK) would add 2 weekss to the current date, for instance
If you want the ad to expire at a certain time of day you can use mktime:
$day = date("d") + ($weeks * 7);
mktime($hour,$minute,$second,$month,$day,$year);
Just to add, some would say that storing the UNIX time as a plain-old int field in the database is the more flexible and portable solution. Furthermore, inserts and updates happen faster because they only involve storing simple integers. It really depends on how much date manipulation you need to do at the database level. I tend to go for portability and do all my date calculations in PHP. To store the current timestamp, I would just insert into an integer column the output of:
strtotime('now');
or
time();
which both return the current (unix) timestamp. Date comparison thereafter can be done by fetching timestamps from the database and performing simple arithmetical operation, as trivial as:
if($tsFromDb > strtotime('+28 days')) {
echo 'it is the future, zombies!';
}
It really depends on what you're using dates for.