Calculating a date - php

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.

Related

Codeigniter : Difference Between two time stamps

I am working in codeigniter. I need to take the difference between two time stamps and use this for link expiry in forgot password service.
So far I have tried.
$date= date('Y-m-d H:i'); this will produce something like 2017-03-20 12:02.
Then again in the near future I want to use the same function to get the currunt time. Eventually measure the difference between these two times in minutes.
$past_time = strtotime('2017-03-20 12:02');
$current_time = time();
$difference = $current_time - $past_time;
$difference_minute = $difference/60;
echo 'Difference in minute between two different Time : '.intval($difference_minute);exit;
You can try with this code. This code for give a difference in minutes between two different Time.
The best way to achieve this is to make an extra column like created_at that contains the datetime of link creation in it. When user click on the link get the current date and time and calculate if the difference is acceptable or not as per your policy.
If you are not using database, then add an extra parameter in your link like created_at or valid_upto with encrypted timestamp in it and use it to calculate the time difference and validate it.
You can save your time in unix timestamp using time() function and use simple subtraction for calculate difference b/w two timestamp. it will return number of second b/w two time then convert second to minute dividing by 60.
OR
you can use strtotime() function to convert date into time stamp.

Select date stored as Unix Timestamp and compare with given Unix TimeStamp

I am stuck for couple of Days on SQL specific scenario. The scenario is as follows,
I have a table, lets call it traffic which has 2 columns -> date and `vehicle (well many more but those are the two I need to match).
The date column is stored as Unix Timestamp. Now this would have been easy to just compare the current date (obtain from php from time() function) however the trick here is that some of these dates have time attached to them also.
For example if you run strtotime(13-02-2017 13:00) and strtotime(13-02-2017) you will get 2 different results. Basically I only care to match the date and not the time.
So I need some way to select the vehicle and date from the database that are equalled to the current Unix Timestamp but with the trick explained above, so I just need to much the date ONLY if possible.
You can use FROM_UNIXTIME() to convert a timestamp to a datetime, and then use the DATE() function to get the date part of that.
WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
However, this can't use an index, so another way that can make use of an index is to check if it's in a range of timestamps for the current date:
WHERE date BETWEEN UNIX_TIMESTAMP(CURDATE()) AND UNIX_TIMESTAMP(CURDATE()) + 86399
(there are 86400 seconds in a day).
SELECT * FROM traffic WHERE DATE(date) = DATE(CURRENT_TIMESTAMP);

Finding the difference between days

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.

How do I evaluate a timestamp in PHP?

For a while I had been using a raw MySQL NOW() function to record the time/date in my MySQL DB until I realized the host's timezone variable was three hours ahead of PST. I've fixed this using DATE_SUB(NOW(), INTERVAL 3 HOUR), but now I have a ton of timestamps that are three hours ahead, and all future timestamps that are the showing the correct time.
Is there a PHP function to evaluate timestamps recorded before I made the fix so I can offset them when they display in my admin utility?
For example:
if($timestamp < 2012-02-16 21:57:18) {
$timestamp - 3 hours;
}
New Timestamp (offset by 3 hours behind)
$timestamp = date('Y-m-d H:i:s',strtotime($row['timestamp_column_name'])-(3*60*60));
Create a second column in your table (perhaps?) and store the offset time - perhaps call it the admin time OR store the admin time offset from the system's time OR you can set the timezone PHP should use using something like the options mentioned here: PHP timezone not set .
the magical function strtotime does all the work for you. seriously check it out for adding, manipulating and even reading human readable forms of dates. Then the date function is good for formatting it back into any form.
For many input formats, strtotime is the way to go. However, its heuristical approach may lead to surprising results, so if you only want to parse a specific format, use strptime.

PHP - Comparing time

I'm working on a calendar/planner web app and I need to compare the start and end times of events before I store them in my DB. An event can only have a range of one day and between 8am and midnight. The start time always has to take place before the end time.
The post values come from the form in the following format hh:mm:ss (12:14:00) etc.. so I can store them in my database without much hassle. Is there any way I can compare these times?
Thanks a lot!
If those times are in the database, comparison operator of the database would works. For example:
SELECT * FROM table WHERE time < NOW()
In PHP, the easiest way to compare times is to convert them to timestamps, and then to compare timestamps as integers. You can use strtotime to do that conversion.
For example:
$time1 = "08:00:00";
$time2 = "09:00:00";
if (strtotime($time1) > strtotime($time2) ||
strtotime($time1) < strtotime("08:00:00")) {
...
}
If you're running PHP 5.3, you can use the diff() method of DateTime objects to get the difference in between two dates. But it's possible to do with just timestamps too (1 day = 86400 seconds)

Categories