Formatting error after finding the difference in time with php? - php

I am looking to find the minutes between two times with php. I have followed the answers I found from other questions on Stack and researched the PHP manual. In my situation, I am getting a DATETIME that is stored in the database and comparing it to NOW. I think my issue lies with a formatting issue. I will post what I have put together.
$order_start = $obj->order_start; //getting detetime from database query
$order_start = DateTime::createFromFormat('Y-m-d h:i:s', $order_start);
$order_start= $order_start->format('Y-m-d h:i:s'); //format detetime
$now_date = date('Y-m-d h:i:s'); //get now time
$diff=date_diff($order_time,$now_date); //get difference
$diff=$diff->format("%i minutes"); //format difference
So whats going on here. The page stops working when I use this line, $diff=$diff->format("%i minutes");
I was under the impression I had to format the date from the database by using, $order_start = DateTime::createFromFormat('Y-m-d h:i:s', $order_start);.
$order_start echo's 2017-07-31 01:44:25
$now_date echo's 2017-07-31 04:43:59
Which looks like the same format but may not be?
Now I'm trying to get the difference between these times and format the result to show minutes.
Thanks for the advice in advance! If there is a better article on here let me know.
I tried
Echo Difference in Two Times
I also read this to try and figure out the formatting.
http://php.net/manual/en/dateinterval.format.php
My Order page

You may convert your dates into timestamp and get the difference, then format it accordingly. Use like this
echo date('H:i:s',(strtotime($now_date)-strtotime($order_start)));
in your case use like this
echo date('h:i:s',(strtotime("2017-07-31 01:44:25")-strtotime("2017-07-31 04:43:59")));

Related

Convert timestamp coming from SQL database to String

I am saving the timestamp in SQL as bigint(20). The number is correct and in android or https://www.epochconverter.com it works fine.
However I am not able to create a date-string based on the timestamp received from database.
First of all, the timestamp seems to come from database as a String, so I can't just say echo date("d.m.Y, $timestamp). The result is the famous 31.12.1969.
So I tried echo date("d.m.Y, strtotime($timestamp)). However, even though strtotime is said to be able to convert almost everything to a timestamp, a simple String containing a timestamp is not possible. Results still remained on the last day of Brian Adams probably favorite year.
Some progress I made by casting the $timestamp to a float value like so: echo date("d.m.Y", floatval($timestamp));. However, now things got really confusing for me. I seemed to have successfully converted my timestamp, however, date() gave me the dates around 02.09.52299.
The timestamps I am using are timestamps of current time, e.g. 1588489252657, which currently leads to the date 23.03.52307.
So all I want is to come to a date based on the timestamp 1588489252657 to see the 03.05.2020 on my screen.
Thanks for any advice!
<?php
$timestamp = 1588489252657; // Timestamp in Milliseconds
$seconds = round($timestamp/1000, 0); // Timestamp Rounded to Seconds
$date = new DateTime(); // Create PHP DateTime Object
$date->setTimestamp($seconds); // Set the Timestamp
echo $date->format('Y-m-d H:i:s'); // Specify the Required Format
The answers are pretty much in the comment sections. But I have shared this answer since this is another approach in OOP fashion. You can leverage the power of PHP's DateTime Class.
PHP Official Documentation For DateTime Class Link Below:
PHP DateTime Class
You have to transform the timestamp to seconds first.
$timestamp = 1588489252657;
$dateInUnixSeconds = round($timestamp / 1000, 0);
$date = \DateTimeImmutable::createFromFormat('U', (string) $dateInUnixSeconds);
echo $date->format('d.m.Y');
PS:
I recommend you to use the \DateTimeImmutable object to avoid mutability problems.
https://github.com/Chemaclass/php-best-practices/blob/master/technical-skills/immutability.md

Reformatting datetime

Ok, so I've got a time string like this:
2013-08-09T15:00:00
Now as far as I can tell, I'll need to convert this to a timestamp, before I can change the formatting - is that correct, or is there a shorter step? For example using one of the classes here:
http://philsturgeon.co.uk/blog/2012/08/why-php-datetime-rocks
I'm not quite sure what the "T" represents (besides time, obviously) and I'm not sure what format that is.
I want to get it into standard 12 hour time.
Use DateTime()
$dt = new DateTime('2013-08-09T15:00:00');
echo $dt->format('Y-m-d h:i:s');

Time format conversion

I need to convert a specific date format into local time (Europe/Sarajevo), I have the time in this format 2013-02-17T15:00:00Z which I don't really understand and this is why I don't know how to convert it to the Europe/Sarajevo time, who knows maybe it is already Sarajevo time, I don't know...
OK I can parse it and remove the T and Z and get a time but these letters mean something, probably they affect the result time...
The result time is for example 2013-02-17 18:00:00, probably there will be a difference due to the letters T and Z which are probably time offset.
Use DateTime. It's much better for working with timezones:
$datetime = new DateTime('2013-02-17T15:00:00Z');
$datetime->setTimeZone(new DateTimeZone('Europe/Sarajevo'));
echo $datetime->format('c');
Reference
DateTime
DateTimeZone
See it in action
You can use php date function like this
$date = '2013-02-17T15:00:00Z';
echo date('Y-m-d H:i:s',strtotime($date));
See the Manual

Trying to convert from seconds to date

Okay so I have an array of results from a table, we have a start and end time we are retrieving. the start/end time would look something like this:
1345497551
Now I'm trying to convert this to a real time, for instance 1345497551 might become 2012/05/09 17:23:12 or something. I've found a few things but none seem to work correctly. one solution I tried, according to what someone was saying on another question on here, was
$createdate = date('H:i:s',$numberofsecs);
where $numberofsecs was the time pulled in from the array. but this only ever outputs 17:00:00 repeatedly for every time we had available for testing.
How can I go about making this work correctly?
Assuming that that's a standard unix timestamp string (seconds since midnight 1/1/1970), then you should be able to use date as you mentioned, but just modify the format string:
echo date('Y/m/d H:i:s', $numberofsecs);
The example you mention where you were always getting 17:00:00 could have been because your test cases were all only datestamps, encoded as timestamps, and having an offset from GMT . . .
I have tried below code:
$ts = 1345497551;
$date = new DateTime("#$ts");
echo $date->format('U = Y-m-d H:i:s');
output : 1345497551 = 2012-08-20 21:19:11

Why my comparison of two dates in PHP is not working?

I have removed the desired output and some styling as they dont affect the question. I cant seem to compare the two dates properly, idea is that if currentDate is greater than deadlineDate there will be no output for that route. What i am trying to do is prevent the system from listing routes which are already closed. I dont understand why its so difficult or then i am missing something very basic here.
<?php
$driveDays = mysql_query("SELECT date,routeid from StopDates where routeid='".$row['id']."' ORDER BY date ASC");
while($stopDates = mysql_fetch_array($driveDays)){
$orderDaysBefore = $row['lastOrderDate']; // How many days before the order must be placed.
// Change the date taken from the query to new format
$originalDate=($stopDates['date']);
$newDate = date("d.m", strtotime($originalDate));
// Count the deadline date for the route.
$deadlineDate = strtotime ("-".$orderDaysBefore." days +12 hours", strtotime ($originalDate)) ;
$deadlineDate = Date('d.m.y G:i', $deadlineDate);
//Get current date which is then compared to the deadline date. Idea is that if currentDate is larger than deadlinedate there will be no input.
$currentDate=Date("d.m.y G:i");
//The line below doesnt seem to be working, i have tried mktime and time too but for some reason it just cant compare.
if (strtotime($currentDate) > strtotime($deadlineDate)){
// Output nothing
}
else { ?>
<p>Output stuff here</p>
<?php
}
}
?>
Problem is that for some rows it hides the route and for some it doesnt. I have tried to do this with mktime and time but i cant seem to figure out what the problem is. Most of the guides i see tell to convert dates to unix timestamp format and if i understand correctly thats exactly what i am trying to do here. Im pretty sure my mistake is a simple one.
Strange thing is that for some dates it seems to work like if deadlineDate is over a month old. deadlineDate forms correctly in the d.m.y G:i format.
Solved
I skipped formatting and compared strtotimes
I added:
$currentDate2=strtotime("now");
$deadlineDate2 = strtotime ("-".$orderDaysBefore." days +12 hours", strtotime ($originalDate)) ;
And then i compare $currentDate2 and $deadlineDate2
Skip the formatting and just compare the results from strtotime().

Categories