This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 9 years ago.
How to compare two dates in php. I got an array of dates from the database and I want it to compare with todays date.
I have something like this.
$d['dd'] = 10;
$d['mm'] = 12;
$d['yy'] = 13;
Now I have done Something like this:
$date = $d['yy'].'-'.$d['mm'].'-'.$d['dd'];
$date_diff=strtotime(date("y-m-d"))-strtotime($date);
if($date_diff>0){
//this date is gone.
}
// I want to show this day will go in 5 days. To do this I have following.
if((strtotime(date("y-m-d", strtotime(date("y-m-d", strtotime(date('y-m-d'))) . "+ 5 days")))-$date_diff)>0){
// This day will go in 5 days time.
}
Now my question is what is the best way to do this or how can I do it in a better(more reliable) way. Any suggestions will be helpful.
Thanks in advance.
When doing date comparision you should do it directly in the database (if possible), as mysql knows about timezones, DST-stuff, leap years etc. PHP DateTime has a history of problems, and if you can get the database to do this I would recommend this.
Save your dates as DateTime and use the built in mysql date-functions: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Related
This question already has answers here:
Add 'x' number of hours to date
(13 answers)
Closed 7 years ago.
I have a form that people are filling in and adding appointment times to, im then inputting the form into a sql db. Im then making a sheet and that shows the appointment slot. All that I need to to is add 1 hour to the appointment slot time. I have a variable call $time that is the appointment time I have tried the following:
<?php
$time=strtotime("+3600");
$final=date("H:i",$time);
echo $final;
but it comes back with all different times any ideas how to make this work?
$time=strtotime("+1 hour");
$time=strtotime("+60 minutes");
Strtotime means: String to time, very useful method when working with dates in PHP.
Or like below answer:
$time = time() + 3600;
Note that you should avoid calculation with strings, that is: A number between quotes. (string)"3600" vs (int)3600
This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 7 years ago.
Hi I'm still new in PHP and currently I want to make system about Employee Allowance and have no idea how to start calculate months in php .For example Steve should get allowance for 4 months from now and database stored the result of calculation, so far the coding is:
?php
$date=date_create("2013-03-15");
date_add($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
?>
Months input is from user and stored in database.
I'm not sure I follow your 40 days versus 4 months, but I think you want something like this:
echo date('Y-m-d', strtotime("Now +40 days"));
This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 7 years ago.
I have a table in the database with the name attendance. It has three columns: intime, outtime and work_hours.
I have date and time in intime and outime. Now I want to write the php code to calculate the time difference in the format hh:mm:ss to store into the column work_hour of attendance table.
Please help me out.
timediff returns the difference between two datetimes as a time value:
UPDATE attendance
SET work_hours = TIMEDIFF (outtime, intime)
I have one simple solution, but you will find better. Just try this.
For this you need to convert both of your outtime and intime in seconds using
$working_time_in_seconds = strtotime($outtime) - strtotime($intime);
echo date('H:i:s', $working_time_in_seconds );
This question already has answers here:
Difference between two dates in MySQL
(14 answers)
Closed 9 years ago.
I've searched but could not find the solution for my case.
One date is recorded at MYSQL,
for example it returns: 2013-10-18 15:42:06 (which format is this ?)
So I need to get the current date (including hours, minutes and seconds).
Then, subtract the MYSQL date - CURRENT date.
The result, i'll set as an jQuery countdown.
Thanks!
This should work:
$seconds_remaining = strtotime('2013-10-18 15:42:06') - time();
Although if you happen to know what timezone the MySQL time corresponds to, then you should append that to the argument passed to strtotime(), e.g., '2013-10-18 15:42:06 GMT'
This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 10 years ago.
I am newbe to php. I have small problem to compare dates in php.
I have 4 dates named date1, date2, start_date and end_date in the format yyyy-mm-dd.
i need the functionality like this:
if((date1>=start_date) && (date2<=end_date)){
}
please help me to solve this.
use strtotime to get the timestamps of your dates - this can be easily compared to each other like you already trying it.
If you have PHP 5.3, this is easily handled by the DateTime and DateDiff classes. If you have not, it is much harder, but look at the 'user contributed' section on DateDiff in the online PHP manual.