I have date value like so 1369195200000 and I tried to convert it to date like so
$date = 1369195200000;
$result = date("Y-m-d", $date);
but it returns like so 45358-01-25 obviously the year is wrong....is there away to get the correct year? The $date is coming from a database.
The time is in milliseconds, it needs to be divided by 1000.
$date = 1369195200000;
$result = date("Y-m-d", $date / 1000);
Related
I have an input file with type time. what I want to do is to get time from the moment now that is if time right now is 2019-11-26 23:50:00 and the value of input field if 22:30:00, I should get a date 2019-11-27 22:30:00.
How can achieve this in PHP? in short, get the datetime stamp for the next occurrence of 22:30:00 which is 2019-11-27 22:30:00 as per the given example.
answer found but can we optimize the code more ?
$a = explode(':', date('H:i:s', strtotime($validateData['time'])));
$str = '+'.$a[0].' hours '.$a[1].' minutes '.$a[2].' seconds';
$trigger_at = date(date('Y-m-d H:i:s', strtotime( $str, strtotime($validateData['date']))));
return $trigger_at;
This is simpler and a lot more readable
$time = "21:30:00"; // Time from input
$today = date("Y-m-d $time");
$tomorrow = date("Y-m-d H:i:s", strtotime($today)+86400);
$date = strtotime($today) < strtotime("now") ? $tomorrow : $today;
Explanation: We take timestamp at specified hour for today and tomorrow, if today timestamp has been passed, we use tomorrow timestamp. Simple. :)
All you are doing is appending (concatenating) a string onto another string.
$time = "22:30:00"; // This is the time you have
$date = date("Y-m-d"); // Right now in yyyy-mm-dd format.
$newdatetime = $date.' '.$time;
That will give you the current date with the supplied time appended to it. You can convert that back into a timestamp using:
$timestamp = strtotime($newdatetime);
The answer below is based on the original question in which the time was assumed to be an offset from now. It is left here simply to avoid deleting a lot of code.
The function strtotime is easy to use for that. However, it doesn't accept HH:MM:SS format. So, you have to alter the string. I would do it like:
$time = "22:30:00"; // This is the time you have
$a = explode(':', $time);
$str = '+'.$a[0].' hours '.$a[1].' minutes '.$a[2].' seconds'; // This breaks it into separate numbers with labels.
$date = date("Y-m-d h:i:s", strtotime($str)); // The adjusted date
You can change the format of the output as you like by changing the first string used in the date function.
I have a date formatted like this: Ymd
I can't seem to find a way to be able to add a number of weeks to this date, I have tried the following:
$quote_start_date = $job['quote_authorised'];
$newdate = date($quote_start_date, strtotime('+5 weeks'));
However the new date is the same, what is the easiest way to add weeks to a date formatted like this?
The 2nd parameter to date takes seconds since epoch. Just add 5 weeks in seconds to the time, ie:
$newdate = date($format, strtotime($quote_start_date) + (5 * 7 * 24 * 60 * 60));
Or just use the constant value "3024000"
$newdate = date($format, strtotime($quote_start_date) + 3024000);
The first parameter of date expects a format for your outputted date string. I think you're looking for the following:
$quote_start_date = $job['quote_authorised'];
$newdate = date("Ymd", strtotime('+5 weeks', strtotime($quote_start_date)));
The more efficient approach would be to use a fixed value for the number of seconds in a week and not rely on PHP parsing an additional strtotime function:
$quote_start_date = $job['quote_authorised'];
$newdate = date("Ymd", strtotime($quote_start_date) + 3024000);
Its a demo :
$date = new Date();
$nextDate = new Date($date.setTime( $date.getTime() + 1 * 86400000 ));
// here 1 is number of day .
I want to get difference between 2 timestamps of format Y-m-d H:i:s in minutes in PHP.
The code used is,
$currentDate = date('Y-m-d H:i:s');
$userLastActivity = date($date);
$timeLapse = (($currentDate - $userLastActivity)/60);
Here, $date is obtained from database.
The value of $timeLapse is 0 in output. Please help.
Try using unix timestamp. Practically it measures the time in seconds from 1/1/1970 and it's a lot easier to use and understand than a php object.
$currentTimestamp = new DateTime()->getTimestamp();
$userLastActivity = date($date)->getTimestamp();
$timeLapse = (($currentDate - $userLastActivity)/60);
You should have the time saved as timestamp on the server too, in that case you could use the $date directly as a number, with no need for a conversion. And also, because it's universal, you can pass it around to javascript or any other language without any worries for conversion
Use strtotime to parse textual datetime into a Unix timestamp and substract $userLastActivity from $currentDate and divide by 60.
See if this helps -
<?php
$currentDate = strtotime(date('Y-m-d H:i:s'));
$date = "2016-10-11 02:40:50";
$userLastActivity = strtotime($date);
echo round(abs($currentDate - $userLastActivity) / 60). " minutes";
?>
For more details :strtotime
Change these 2 lines for a start, as I don't think dividing dates by 60 is gonna work to well.
$currentDate = time();
$userLastActivity = strtotime($date);
That way you have time stamps and not dates (string)
I'm writing code to subtract two dates. It is for a contract type thingy, where user gets to see the number of days left for his contract to complete. Something like start_date_time="today" and end_date_time=y where the value of y is retrieved from the database (DATETIME type). It is in the mysql datetime format (yyyy-mm-dd HH:mm:ss).
<?php
include_once '../include/connections.php';
$id =$_REQUEST['uid'];
$result= mysql_query("SELECT * FROM data WHERE uid = '$id'");
$test = mysql_fetch_array($result);
echo $test[14];
echo "<br /><br />";
$today=time();
$enddate=strtotime('$test[14]');
$timediff = $enddate - $today;
$days=intval($timediff/86400);
$remaining=$timediff%86400;
$hours=intval($remaining/3600);
$remaining=$remaining%3600;
$mins=intval($remaining/60);
$secs=$remaining%60;
echo "<br>".$days.' days '.$hours.' hours '.$mins.' minutes and '.$secs.' seconds.';
?>
When I echo $test[14];, I get the date and time as stored in the database which is
(2012-09-26 00:00:00)
When i echo $today; then i get it in this format 1348381896. Now, how do i convert this format to the one retrieved from the db so that i can subtract the 2 dates and get the number of days and time left?
You can use these 2 functions to convert dates to each other,
Use this for timestamp to MySQL datetime:
$timestamp = '1348381896';
$date = date('Y-m-d H:i:s', $timestamp);
echo $date;
Use this one for MySQL datetime to timestamp:
$date = '2012-09-26 00:00:00';
$timestamp = strtotime($date);
echo $timestamp;
Also if you are willing to Subtract your dates in MySQL side, you can use the DATEDIFF and or TIMEDIFF functions:
Also you can work with timestamps in MySQL too, using TIMESTAMPDIFF and UNIX_TIMESTAMP functions ...
You could use PHP's DateTime classes for this:-
$today = new DateTime();
$mysqlDate = "2012-12-15 13:40:20"; //for example
// To match your code this would be $mysqlDate = $test[14];
$mysqlFormat = 'Y-m-d H:i:s';
$endDate = DateTime::createFromFormat($mysqlFormat, $mysqlDate);
$diff = $today->diff($endDate);
echo "You have {$diff->d} days, {$diff->h} hours and {$diff->m} minutes left";
This will give the following output:-
You have 22 days, 6 hours and 5 minutes left
(This will change depending on when you run the code).
$diff is an instance of DateInterval.
date formats acceptable to DateTime::createFromFormat() can be found here http://www.php.net/manual/en/function.date.php
I have data in my database that returns the month/day/year data points for events.
What I want to do is check whether today is 20 days after the month/day/year that I get.
So far I have something like:
// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];
// Get today's date
$todays_date = date("Y-m-d");
// Create the date I am comparing with
$date_string = $year.'-'.$month.'-'.$day;
My question is how do I compare it? And is the format going to matter in comparing the two dates?
Thanks!
Parse the month/day/year into a DateTime:
$other = date_create($year.'-'.$month.'-'.$day);
Add 20 days to it:
$twenty_days_after = clone $other;
$twenty_days_after->modify('+20 days');
Compare it with today:
if ($today >= $twenty_days_after) {
// today is 20 days after the month/day/year that you got
}
See date_create.
$database_date = strtotime(sprintf('%s-%s-%s', $year,$month,$day));
$twenty_days_from_now = strtotime('+20 days');
$twenty_days_From_database = strtotime(sprintf('%s-%s-%s +20 days', $year,$month,$day));
Using strttime is the easiest method.
use
strtotime()
function which will convert your dates into timestamp and you can compare them easier
// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];
// Get today's date
$todays_date = date("Y-m-d");
// Create the date I am comparing with
$date_string = $year.'-'.$month.'-'.$day;
if ( date( 'Y-m-d', strtotime( "+20 days", $date_string ) ) {
//date is 20 days before today
}
strtotime does magical things