Subtract two dates in PHP / Mysql [duplicate] - php

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'

Related

Return current date + 7 days in timestamp form [duplicate]

This question already has answers here:
Return current date plus 7 days
(10 answers)
Closed 2 years ago.
So I have the below property where I'm passing in a time()-86000 value, but what would be the best call to generate a timestamp from the time the class method executes and then adds 7 days to that.
Here is what I got:
$profile->set_picture_expiration(time()-86000)
Would using time()-86000 be the right call? I'd like to write it to the DB in timestamp format from the current time + 7 days.
First of all, if you need +7 days, why are you using minus?
Next, 7 days are 7*24*60*60 = 604800 seconds, not 86000.
Finally, the easiest way to get the timestamp for such relative dates is using the strtotime function. In your specific case it would be strtotime('+7 days').
$profile->set_picture_expiration(strtotime('+7 days'));

How to convert a date in the format DD-MM-YYYY_HH-MM-SS in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
Here are 3 examples of date and time strings I have
08-01-2019_03-00-34pm
22-01-2019_02-05-06pm
30-01-2019_11-17-07am
I would like to convert these to Unix Time Stamps
For example I have tried this on the first one..
echo (strtotime("08-01-2019"));
This half works, as it gives me a timestamp of 1546905600 which translates back to the correct date, but obviously makes the time 12:00:00am by default. So I'm stuck with how to deal with the time bit
I understand there is a str_replace() function so as long as I know what format I need to make the time bit, I guess I could use that?
Use DateTime::createFromFormat():
$date = DateTime::createFromFormat('d-m-Y_h-i-sa', '08-01-2019_03-00-34pm');
You can then use it to get the timestamp
$date->getTimestamp(); // 1546977634

How to calculate time difference in PHP and store the time difference in mysql database [duplicate]

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 );

create time stamp and compare time by mysql field [duplicate]

This question already has answers here:
MySQL convert date string to Unix timestamp
(4 answers)
Closed 8 years ago.
there is a table with these fields
month
year
day
and these files are contain numbers like
year = 2001 and month = 10 and day = 25
and i have time stamp in php
so i need to compare these in mysql
something like:
select * from times where mktime(year,month,day)<1235478554
is there any function in mysql for this?
i need to make time stamp in mysql with the mysql fields...
UPDATE 1 :
i used like this and not worked
SELECT * from work_result where UNIX_TIMESTAMP('year-month-day 00:00:00')<1
UPDATE2:
There's UNIX_TIMESTAMP which will get you the Unix timestamp of a given date string. Which means you need to build a date string inline in your MySQL query. Doing so is considered bad style, you should propably be storing a Unix timestamp within your database.

Extended hours in php [duplicate]

This question already has answers here:
Calculate time elapsed in php - spans over 24 hours
(2 answers)
Closed 9 years ago.
I have been searching for a while now regarding on how to format hours in php that will show 000:00:00 instead of 00:00:00.
I need this to calculate the duration of records with start and end Date with a datetime data type in mysql.
The problem with my current format ('H:m:s') is it only count 24 hours and when the duration goes higher than that, it will show 01:00:00 instead of 25:00:00.
Try TIMEDIFF() function in MySQL
Example:
SELECT TIMEDIFF('2008-12-31 23:59:59.000001',
'2008-12-30 01:01:01.000002');
-> '46:58:57.999999'

Categories