I have two times saved in database as
DayTime1 = "Wed 09:00"
DayTime2 = "Wed 13:00"
I want to get the difference between these two dates in minutes.
TIMESTAMPDIFF(MINUTES,DayTime1,DayTime2) return null
I'm not able to get through it. Is there any way to get difference?
Please note, that SELECT STR_TO_DATE("Wed 09:00", "%a %H:%i") returns 0000-00-00 09:00:00 (at least on my local MySql 5.5.16). So if comparing different days, you won't get the correct result.
If given a year and week, the day name will be interpreted to a real date, so comparisons may also span days. For example (though not really elegant, I admit):
SELECT TIMESTAMPDIFF(MINUTE,
STR_TO_DATE(CONCAT(YEAR(CURDATE()), WEEKOFYEAR(CURDATE()), ' Tue 09:00'), '%x%v %a %H:%i'),
STR_TO_DATE(CONCAT(YEAR(CURDATE()), WEEKOFYEAR(CURDATE()), ' Wed 13:00'), '%x%v %a %H:%i')
)
Since you also included the php-tag, I'm assuming a PHP solution is valid as well:
$daytime1 = "Wed 09:00";
$daytime2 = "Wed 13:00";
$diff = abs(strtotime($daytime1) - strtotime($daytime2)); //absolute diff in seconds
$diff = $diff / 60; //Difference in minutes
EDIT:
And here is a pure MySQL solution:
SELECT ABS(
TIME_TO_SEC(
TIMEDIFF(
STR_TO_DATE("Wed 09:00", "%a %H:%i"),
STR_TO_DATE("Wed 13:00", "%a %H:%i")
)
)
) / 60
The second parameter, "%a %H:%i"is the date format. If it's always in the format of "First three letters of weekday, space, hour with leading zero, :, minutes" then you can use this format.
Related
I have a problem with my script and I dont understand where is the problem. So I have this code :
$i_now = strtotime(date('Y-m-d'));
$i_date_last_bonus = strtotime($o_member->date_last_bonus);
$i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400);
print_r("Date Now :".date('Y-m-d'));
print_r("Last Win :".$o_member->date_last_bonus);
I get the $i_datediff = 1 and I dont understand why because in the print_r I have Date Now :2015-12-04 and Last Win:2015-12-03
Can you help me please where I make the error ? Thx in advance and sorry for my english
In one day there are 24 Hrs, in each hour there are 60Min, in each min there are 60sec. Hence, there are 24*60*60 = 86400sec in one day.
Now, The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Means it returns seconds.
so, i_now = 1449187200 andi_date_last_bonus = 1449100800
Difference is 86400sec.
Now $i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400); this is converting seconds in days.
And Difference is 86400 / 86400 = 1
means 1 day.
This result is correct as you get the number of seconds between two dates first and then you divide it by the number of seconds in 24 hours (86400) which gives you 1 (day) as a result.
I am trying to set publish date based on user choice and give it interval in a loop. But after it is substituted with the intervals, the year changed to the current year.
Here is the sample of my code:
$datestart = "2012-03-06";
$datenow = date("$datestart H:i:s", current_time( 'timestamp' ));
$newpostdate1 = $datenow + strtotime("0 years 0 months 1 days 0 hours 0 minutes 0 seconds");
$newpostdate = date("Y-m-d H:i:s", $newpostdate1);
echo $datenow . " " . $newpostdate;
$datenow Will return 2012-03-06 16:19:33 while $newpostdate return the current date plus 1 day i.e: 2014-03-15 17:02:23.
Why $newpostdate returning the current date plus next 1 day instead of 2012-04-06 16:19:33 ?
..because what you're doing doesn't do what you think it does.
First, you set $datenow to a string (not a date object), with value "2012-03-06 " + the current time (assuming that's what current_time returns).
Then you call strtotime with the value "1 days" (well, your string has a bunch of other zero-valued fields, but they don't change the result), which returns the current time + 24 hours as a number (the number of seconds since 1970).
Then you take that value and add it with + to the above string. This causes the string to be interpreted as a number, so it turns into 2012 (and the rest of the string is ignored). So the result is a timestamp representing the current time + one day + 2,012 seconds - or one day, 33 minutes and 32 seconds from the time the code is run. Which you then format as a string.
You could use John Conde's solution to get a more meaningful result. (I assume your real problem is different, else why not just start out by setting the string to '2012-03-07' in the first place?)
The first parameter of date() is the format you want the timestamp passed as the second parameter to be displayed as. So basically you are using date() incorrectly.
I think this is what you are looking for:
$date = new DateTime($datestart);
$date->modify('+1 day');
echo $date->format(Y-m-d H:i:s);
I've two timestamps with a gap between a few hours and a few days.
Is there a way in PHP and / or MySQL to get the time slice for every day?
INPUT
Timestamp 1: 2013-10-30 10:00:00
Timestamp 2: 2013-10-31 11:00:00
OUTPUT
2013-10-30: 14 hours
2013-10-31: 11 hours
Easiest way to perform operations on dates is to convert them to timestamps.
Here, what you can do is to get three timestamps :
$ts1 = strtotime("2013-10-30 10:00:00");
$ts2 = strtotime("2013-10-31 00:00:00");
$ts3 = strtotime("2013-10-31 11:00:00");
$first_time_slice = ts2 - ts1; //here you got your first time slice in seconds, convert it to hours
$second_time_slice = ts3 - ts2; //idem for the second time slice
It can be done in MySql like this:
SELECT TIMEDIFF(TIMESTAMP(DATE('2013-10-30 10:00:00') + INTERVAL 1 DAY), '2013-10-30 10:00:00'),
TIMEDIFF('2013-10-31 11:00:00', TIMESTAMP(DATE('2013-10-31 11:00:00')))
Probably I do something wrong but I can't see the point of it ....
Today is a one day of the week. I must know - how many days have elapsed from last monday to today?
$date_1d = date('Y-m-d', strtotime ('last Monday'));
// last monday
$date_today = date('Y-m-d');
// actual date, today
$ile_dni = (strtotime($date_today) - strtotime($date_1d)) / (60*60*24);
// difference in days - how many days have elapsed from today to last monday
I have 2 identical scripts as above in the same directory on the same server
first said:
&date_1d = > 2012-03-19
$date_today => 2012-03-26
strtotime($date_today) = > 1332720000
strtotime($date_1d) = > 1332115200
$ile_dni = > 7
second said:
&date_1d = > 2012-03-19
$date_today => 2012-03-26
strtotime($date_today) = > 1332712800
strtotime($date_1d) = > 1332111600
$ile_dni = > 6.9583333333333
Couse the right answer is the first. And what to do with this situation?
use ceil() on the $ile_dni the differences are from the timestamp generated out of 'last Monday' and strtotime and i'm sure there is a better way to do this with date('N'), you could do
$ile_dni = date('N')-1;
if you wanted ile_dni to be 0 on monday, 1 on tuesday etc..
Did you run those two scripts at the same time? I'm guessing there's a timezone/DST issue going on. Note that your method of computing date differences is very vulnerable to these kinds of issues. If you're using PHP 5.3 or higher, use date_diff.
I have a general question on calculating dates with php.
What happens if I store a timestamp like this in my database:
$db_timestamp = '2010-01-31 00:00:00';
and then run a daily script that checks if a month has passed since the timestamp was saved in the database:
if ($db_timestamp == make_unix_timestamp(mktime(0, 0, 0, date("m") - 1, date("d"), date("Y")), TRUE, 'eu')))
{
do something
};
my problem is that i just realized that this wouldn't work for all dates. in this case 'do something' would not be called in February, since February doesn't have a 31st day. any idea on how to implement something like that?
First, your DBMS should have a data type for date/time. They all store timestamps in a similar way.
MySQL then provides a function called UNIX_TIMESTAMP() if you need to return a timestamp PHP can understand.
SELECT UNIX_TIMESTAMP(`createTime`) FROM `articles`;
The opposite function is called FROM_UNIXTIME():
INSERT INTO `articles` (`createTime`) VALUES ( FROM_UNIXTIME(12345678) );
MySQL (or another DBMS for that matter, but I'm using MySQL as an example) has a slew of other functions to calculate time differences. For example, to know if an article is more than one month old, use can use DATE_SUB():
SELECT * FROM `articles`
WHERE `article`.`createTime` <= DATE_SUB(NOW(), INTERVAL 1 MONTH);
(In MySQL5 and above, you can also write it as such)
SELECT * FROM `articles`
WHERE `article`.`createTime` <= (NOW() - INTERVAL 1 MONTH);
$ts = strtotime($db_timestamp);
if ($ts < (time() - 2592000))
{
do something;
}
2592000 seconds = 30 days
You could use date_diff http://us3.php.net/manual/en/datetime.diff.php
or do a comparison of the timestamp in your database with
strtotime("-1 month");
You could check the timestamp using a query:
MySQL:
select date from table where date < now() - INTERVAL 1 MONTH;
It kind of depends on how you consider "one month".
If "one month" means "30 days", a solution would be to compare the timestamp you get from the database with the current timestamp :
$db_timestamp = strtotime('2010-01-31');
$current_timestamp = time();
var_dump( ($current_timestamp - $db_timestamp) / (24*3600) );
If the difference is 30 days... that's it.
A couple of notes :
strtotime converts a date to an UNIX timestamp-- i.e. the number of seconds since 1970-01-01
time returns the current UNIX timestamp
you can compare timestamps : they only represent a number of seconds ; and there are 24*60*60 seconds per day ;-)