i'am trying to format a mysql datetime-value to another format for the view.
I tried several methods, for example:
$date = new DateTime($datetime, new DateTimeZone('Europe/Berlin'));
return $date->format("Y-m-d H:i");
But it doesn't work.
The problem is, that (i think..) PHP manipulates the value. The output is always the current datetime and not the value which is saved in the database. For example "2011-04-21 22:27:42".
Does anyone has an idea how the solve this problem?
Greetings.
My guess is that $datetime isnt set
The first parameter of DateTime is defaulted to now if it's not set
http://www.php.net/manual/en/datetime.construct.php
Related
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
So,
I know a lot of requests and question has been askeb about this subject but none really worked for my case... I'm working on a liscensing api with php (supposed to be easy) and I get a string date (2000-01-01) from my db and the length of the subscription. So I'm creating a DateTime Object with it using this :
$created_at = date_create($result["created_at"]);
date_add($created_at, date_interval_create_from_date_string($result["length"]." days"));
But for some unknowed reason, It seems I can't get the current date in a DateTime object so I can just compare them with <>=. Even if I use date_sub() or date_diff() It still require two DateTime object. I'm really deseperate at this point so I figured I could ask for some help.
Hope I didn't miss anything obvious
You can use the 'now' attribute,
$today = new DateTime('now'); to get the current time.
Don't forget to set your timeregion in your php.ini to be able to get the right time.
And if you want to compare them, you can use date_diff and then
$var->format('%r') to get the value.
%r is going to be empty if the result is positive.
Good luck!
I've got a variable declared like such: $var = new DateTime(null);.
I want to change the time that echo $var->format('g:i A'); outputs.
I want to achieve this solely by using a time string such as 07:30:28.
How can I do this without re-creating a DateTime object ($var) each time? I can't think of a way to achieve this.
DateTime::modify() will do exactly what you want. It will quite happily accept a time in string format and apply it to the object:-
$date = new \DateTime();
$date->modify('07:30:28');
See it working.
Alternatively, you can do it all in one go:-
$date = (new \DateTime())->modify('07:30:28');
I create an object
$date = new DateTime();
It is set to current date 2011-04-01 21:43:40. I try the following
$date->modify('midnight');
I expect the object to set to 2011-04-01 00:00:00. But nothing happened. Object hadn't beed modified, and continue to have a 2011-04-01 21:43:40 date. I just want to reset the time to midnight (00:00:oo).
http://codepad.org/w5RAF0Lh
This piece of code (with midnight) will not work without date.timezone setting
UPDATE: this piece of code requires PHP 5.3.6 to work correctly. In previous versions DateTime::modify('midnight') didn't work
Got a few questions, perhaps the will help illuminate the problem...
Is a timezone set in your php.ini file?
After you create the new DateTime() object are you using var_dump() or some other function to view its parameters and get the set date?
Have you tried and been successful passing other date and time formats into the modify method?
Doctrine checks if the DateTime object has changed its reference.
Modifying an object doesn’t change the reference, so for doctrine, this is not a change.
Use new \DateTime('midnight') instead.
I had the same problem!
However the returned date was correct, so what I did is:
#$date->modify('midnight');
Solved using
$date = new DateTime(date('Y-m-d H:i:s', strtotime('today midnight')));
echo $date->format('Y-m-d H:i:s');
The values that I received from my device are: 090211 = ddmmyy and 062123 = hhmmss in UTC.
But I found that the time is always 8 hours later if compared to the time that I need. It is because the time for Malaysia is +8:00. First I would like to add 8 hour, and finally I would like to store this kind of date format into my MySQL database as "2011-02-09 06:21:23". How would I convert these values?
To convert in PHP to a datetime, you will need the function DateTime::createFromFormat(); This function will return a DateTime.
Using this function you can also pass the timezone as a parameter.
Example:
$date = DateTime::createFromFormat( 'dmy Gms', '090211 062123', new DateTimeZone("Europe/Amsterdam") );
You can create a output a following:
echo $date->format('Y-m-d H:i:s');
or UNIX timestamp for the MySQL:
echo $date->format('U');
Hope this helps!
PHP has both localtime and gettimeofday functions, are you by chance using the wrong one (or misinterpreting its results)?