I want to get time between dates in secounds and here is my code:
$rezultat = $polaczenie->query(sprintf("SELECT terminstart FROM uzytkownicy WHERE id=%d", $_SESSION['id']));
$wiersz = $rezultat->fetch_assoc();
$start = DateTime::createFromFormat('Y-m-d H:i:s', $wiersz['terminstart'])->format('Y-m-d H:i:s');
echo'<script>alert("'.$start.'");</script>';
$teraz = (new \DateTime())->format('Y-m-d H:i:s');
echo'<script>alert("'.$teraz.'");</script>';
$interval = $start->diff($teraz);
echo'<script>alert("'.$interval.'");</script>';
I see from alerts, that $start is working good, $teraz (means now) is working good but I even don't get 3th error with diff. What is the problem?
$start and $teraz are strings, not DateTime objects because you call DateTime::format() on them. You can only do a diff on DateTime objects.
$start = new DateTime($wiersz['terminstart']);
$teraz = new \DateTime();
$interval = $start->diff($teraz);
Also:
Don't use javascript for debugging. That's kinda insane.
You don't need DateTime::createFromFormat() if you date is already in an acceptable format (and Y-m-d H:i:s is an acceptable format)
Related
Here $current is the current timestamp and $added_time is the timestamp from when the item is added into the database. Both of them when echoed separately gives the correct o/p but when i try to calculate the difference between them using diff() function, the o/p i get is "Y-m-d H:i:s". What is happening?? I got no clue.
$current = new DateTime();
$diff = $current->diff($added_time);
echo $diff->format('Y-m-d H:i:s');
The diff() method returns a DateInterval object holding the difference of the dates but the usual form Y-m-d H:i:s won't work as is; you need to prefix each format specifier with a percent symbol %
$current = new DateTime();
$diff = $current->diff($added_time);
echo $diff->format('%Y-%m-%d %H:%i:%s');
I cannot use DateTime because of the version of PHP I am running. Can anyone suggest a way to get the equivalent value using strToTime or some other date function. I know this is probably an easy question but I am very rusty on dates in php.
i.e. something like
$date = strToTime('today');
where $date is a date that I can then manipulate by adding hours and so forth along these lines...
$start = $date->format('Y-m-d H:i:s');
$date->modify('+ 60 minutes');
$end = $date->format('Y-m-d H:i:s');
Thanks in advance for any suggestions.
$startTime = strtotime('now');
$endTime = strtotime('+60 minutes', $startTime);
$start = date('Y-m-d H:i:s', $startTime);
$end = date('Y-m-d H:i:s', $endTime);
You can pass a 2nd parameter to strtotime that will be the time that is used when the calculation is made. By default it is time().
strtotime
There are alternatives, that you can get from github, just type php date in search.
For example, you can use moment.php library, which is a clone of moment.js library which is intented to fix problems among different systems, versions etc...
And to perform date calc in moment.php
For example
$m = new \Moment\Moment('2012-05-15T12:30:00', 'CET');
echo $m->addMinutes(60)->format(); // 2012-05-08T13:15:00+0200
I need to set timestamp eg. 4 hours ahead and 2 hours ahead separately
In my database, I have their columns as timestamp.
I know I could do something similar to this but am not sure if it's correct.
// For 4 hours ahead of time
$dt2 = date("Y-m-d 04:i:s");
//For 2 days ahead
$dt2 = date("Y-m-02 H:i:s");
//For 4 hours ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+4 hours'));
//For 2 days ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+2 days'));
In my mind it's much better to work with DateTime field and the DateTime class.
You have the ability so modify that objects very easily. For example:
$aktDate = new \DateTime();
Now you have the actual date and time in an object. If you want you can put a string insight the DateTime function so set your date manually.
$aktDate = new \DateTime('Y-m-d 04:i:s');
Not you can modify your dates if you want with the modify function.
in your case:
$pastDate = clone $aktDate;
$pastDate->modify('+2 days');
$futureDate = clone $aktDate;
$futureDate->modify('+4 days');
if($pastDate < $aktDate && $aktDate < $futureDate) {
// do something
}
I like the DateTime function much more because it's readable and you can work directly with your DateTime fields from your MySQL database if you have such fields. You can write that example much shorter but so you have better readability.
$date = new DateTime('now');
$date->modify('+2 days');
echo $date->format('Y-m-d H:i:s');
$date = new DateTime('now');
$date->modify('+4 hours');
echo $date->format('Y-m-d H:i:s');
You need to use the strtotime() function (http://php.net/manual/en/function.strtotime.php).
For your examples:
//+2 hours<br>
strtotime("+2 hours");
// +2 days<br>
strtotime("+2 days")
Edit: for what you ask, about posted values, the syntax is like this:
strtotime("+2".$_POST['field_name']." days");
You can use hours/days/months/weeks/years and either + or -
I receive a string representing a date in the format YmdHis. When I turn it into a DateTime object and print it back out I get the timestamp for "now". Why is this happening?
$time = '20140718121314';
$format = 'YmdHis';
$dt = new DateTime();
$dt->setTimeZone(new DateTimeZone('UTC'));
$dt->createFromFormat($format, $time)
$dt->format($format); // equals 20140731001832 (i.e. 'now')
I figured it out. It's an order of operations issue. The timezone has to be set after DateTime::createFromFormat().
createFromFormat is static method. Example of usage:
$dt = DateTime::createFromFormat($format, $time);
About static methods
This is my current code:
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year");
$arrivalTime = date('Y-M-d H:i:s', strtotime("+$flightTimeMin minutes", $dateGame));
This isn't working because I believe "$dateGame" is an object. How would I turn it into something readable by "strtotime"?
Thanks
You dont need to use strtotime() since you are using datetime and you can use datetime object to format the date as
$universeTime = 3 ;
$flightTimeMin = 20;
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year +$flightTimeMin minutes");
If you want to format the display you can use as
echo $dateGame->format('Y-m-d H:i:s');
or just use
echo $dateGame->date ;