I'm wondered why PHP DateTime class accepts a strange value to represent the timestamp?
The stranger value is #{$timestamp}:
Example:
$timestamp = time();
$date = new DateTime("#{$timestamp}");
PHP's Manual does not show any information related to #{$timestamp}!!
Please note that it's not possible to do something like this:
$date = new DateTime(time());
I know the easiest way to set a timestamp for DateTime class is: $date->setTimeStamp(time()); but I'm asking about #{$timestamp}
Does anyone know what is the magic behind #{$timestamp}?
Use of #timestamp is documented under Date/Time>Supported Date and Time Formats>Compound Formats>Localized Notations
Description Format Examples
Unix Timestamp "#" "-"? [0-9]+ "#1215282385"
Related
I'm working with Laravel 5.8 and I wanted to show a popup message if the UNIX timestamp of the current date is equal to the defined Unix timestamp of the popup.
So in order to do that, I added this at the Controller:
$date1= $popup->datep; // returns 1636403400
$date1 = Carbon::createFromFormat('Y-m-d H:i:s', $date1);
dd($date1);
But instead of getting the result of $date1, I get this error:
The separation symbol could not be found Data missing
So what's going wrong here? How can I solve this issue?
You are specifying a format that is clearly not an unix timestamp. Use method for the timestamp.
$date = Carbon::createFromTimestamp($popup->datep);
If you want to compare it to be the same date, you should do the following. I don't assume you want to compare it by the hour or second, that those will almost never match.
$date->startOfDay()->eq(now()->startOfDay());
Regarding Carbon Docs:
createFromFormat() is mostly a wrapper for the base php function DateTime::createFromFormat.
which is means that your second parameter must be a valid date/time format, not a timestamp.
The DateTime::create docs:
$datetime
String representing the time.
Instead, you need to use the createFromTimestamp instantiator.
$date1 = Carbon::createFromTimestamp($date1);
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
I have a multi national system that uses two different types of date formats. One uses "d-m-Y" the other uses "m-d-Y". I have a Local class that is responsible for creating a DateTime object based on a date string passed into it.
The string being passed in is dynamic and can be either version. The problem is that even if you specify the DateTimeZone in the DateTime constructor you still have to pass in a string based on 'm-d-y'.
I need to be able to pass a string to the DateTime constructor based on the DateTimeZone that is passed in along with it. For example, if my TimeZone is set to Australia/Sydney
the DateTime constructor should accept a string like '31/11/2017' but it doesn't. The DateTime constructor doesn't take into account the TimeZone passed in with the string. I would have to use DateTime::createFromFormat, but this means I would have to manually specify a format for hundreds of time zones. It would be much easier if the DateTime constructor would take a string format based on the time zone passed in like this...
$dateTime = new DateTime('31/11/2017', new DateTimeZone('Australia/Sydney'))
This should work but doesn't in my case. Does anyone know what I am doing wrong? There must be a way to achieve this.
Maybe the format for your date isn't accepted by PHP's DateTime constructor. For a workaround try this:
$dateTime = DateTime::createFromFormat('d/m/Y', '31/11/2017')
->setTimeZone('Australia/Sydney');
or
$dateTime = DateTime::createFromFormat('d/m/Y', '31/11/2017', 'Australia/Sydney');
you can use the php-intl-extension to get default strings per locale-string
foreach (["en_US", "en_IE", "de_DE"] as $fmt) {
$formatter = datefmt_create($fmt, IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
echo datefmt_get_pattern($formatter) . "\n";
}
see the documentation on datefmt_get_pattern for more details.
I have a date in this format: 20101101120000
I need to convert it to a timestamp with PHP.
I've been searching the PHP docs online, but can't find anything that can convert directly. Does one exist? If not, what's the most efficient way to do the conversion? Thank you for your help.
You can do this with DateTime::createFromFormat:
$d = DateTime::createFromFormat('YmdGis', '20101101120000');
$d is now a DateTime instance. You can either convert it to a timestamp with $d->getTimestamp() or use the DateTime methods on it.
Note that this requires PHP 5.3.
strtotime('20101101120000')
....
You need the function strptime.
The formats are described at strftime.
I only add to this resolved question because this may be helpful for users who stumble upon here (like I did) and are looking to get an actual datetime timestamp.
My assumption for most folks when they mention timestamp is that they're looking for a normal "YYYY-MM-DD HH:MM:SS" timestamp not a unix timestamp which you get when you use the getTimestamp() method (see accepted answer if you are indeed looking for the UNIX Timestamp).
So I will further elaborate on lonesomeday's answer (which is fully correct) for those looking to actually get a valid formated date that they can display to users or insert into their database:
$d = DateTime::createFromFormat('YmdGis', '20101101120000');
$formatedDate = $d->format('Y-m-d H:i:s'); // will return 2010-11-01 12:00:00
$formatedDate = $d->format('m-d-Y h:i A'); // will return 11-01-2010 12:00 PM
To change 2009-12-09 13:32:15 to 09/12/2009
here:
echo date("d/m/Y", strtotime('2009-12-09 13:32:15'))
You can use strtotime to get the timestamp of the first date, and date to convert it to a string using the format you want.
$timestamp = strtotime('2009-12-09 13:32:15');
echo date('d/m/Y', $timestamp);
And you'll get :
09/12/2009
[edit 2012-05-19] Note that strtotime() suffers a couple of possibly important limitations:
The format of the date must be YYYY-MM-DD; it might work in some other cases, but not always !
Also, working with UNIX Timestamps, as done with date() and strtotime() means you'll only be able to work with dates between 1970 and 2038 (possibly a wider range, depending on your system -- but not and illimited one anyway)
Working with the DateTime class is often a far better alternative:
You can use either DateTime::__construct() or DateTime::createFromFormat() to create a DateTime object -- the second one is only available with PHP >= 5.3, but allows you to specify the date's format, which can prove useful,
And you can use the DateTime::format() method to convert that object to any date format you might want to work with.
Using the date() method.
print date("d/m/Y", strtotime("2009-12-09 13:32:15"));
$long_date = '2009-12-09 13:32:15';
$epoch_date = strtotime($long_date);
$short_date = date('m/d/Y', $epoch_date);
The above is not the shortest way of doing it, but having the long date as an epoch timestamp ensures that you can reuse the original long date to get other date format outputs, like if you wanted to go back and have just the time somewhere else.