How can I convert string "M-d-Y::H:i:s" to unix timestamp in php. I just realized manipulating date and time must be the most time consuming entity in any programming language.
Trivial Question: Why do you think there isn't any universal date time format. Why are there so many variations of same data? AArrhh.
The OOP way (requires PHP >= 5.3.0):
$dt = DateTime::createFromFormat("M-d-Y::H:i:s", $input);
$ts = $dt->getTimestamp();
You should of course check the return value of createFromFormat (it's false if an error occurs) but you should also definitely check DateTime::getLastErrors(); otherwise you might be surprised if e.g. your input has a day of "Jan 32". See my answer here for more info.
i have this issue service unavailable (with message) ResponseText: Error: Call to a member function getTimestamp() on boolean How to fixed this issue.
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'm a bit confused as to why the date formatter T does not always return a timezone abbreviation.
The following code Carbon::now()->timezone('Europe/London')->format('T') returns 'BST', Carbon::now()->timezone('Europe/Stockholm')->format('T') returns 'CEST', but Carbon::now()->timezone('Asia/Bangkok')->format('T') returns '+07', which is rather confusing for the users of a scheduling system I'm building.
I would expect T to always return a 'non-numeric' indication of the timezone, is there a way to achieve this without having to resort to writing out 'Asia/Bangkok'?
It should return "ICT". It's a known bug of PHP: https://bugs.php.net/bug.php?id=74835
You get the same result without Carbon:
$date = new DateTime('now', new DateTimeZone('Asia/Bangkok'));
echo $date->format('T');
Is there a way for me to tell the strtotime() function not to change the time I give it into BST? i.e. if do
date('g.ia', strtotime("2014-06-25T19:30"))
I want to get 7:30pm, just as if I entered
date('g.ia', strtotime("2014-06-25T19:30"))
(The first one currently returns 6:30pm)
I'm aware I could just write a manual check for the day/month and add an hour if necessary, or just parse the time myself from the string, but both solutions sound a bit messy (I'll have to do this in quite a few places).
Sorry if there's something obvious I'm missing, pretty new to php
Function date() will format time based on your timezone setting. Said that, your example doesn't make sense since strtotime() will use current timezone setting to convert input to unix timestamp, and then function date() will use again that timezone setting to format timestamp back. You must be changing timezone setting between strtotime() and date() function calls, like this demo.
You can simply use DateTime extension, where you implicitly tell in what timezone is your time:
$dt = new DateTime('2014-06-25T19:30', new DateTimezone('Europe/London'));
echo $dt->format('g.ia');
I've been using setTimeStamp to convert a Unix Timestamp to a datetime in the following way:
$startHireConverted = strtotime($startHire); // converts start hire to time
$endHireConverted = strtotime($endHire); // converts end hire to time
$startdt = new DateTime();
$startdt->setTimeStamp($startHireConverted);
$mysql_startdate = $startdt->format("Y-m-d H:i");
This was working nicely, but recently I've put the website live and the version of PHP can only be 5.2.12 which doesn't support the setTimeStamp method.
I've tried changing setTimeStamp to format which is getting rid of the errors and converting the datetime but it is changing it to the current datetime - 5 hours for some reason rather than the date stored in $startHire.
$startdt->format($startHireConverted);
Any ideas on how to get around this problem?
$startHire starts out as a string version of datetime.
Thankyou
I don't think you need to do the step with unix timestamp at all:
$startdt = new DateTime($startHire);
PHP manual: DateTime::__construct
But that might depend on the format you're getting $startHire in. See Supported Date and Time Formats.
I display the date or the time on my website a lot and I'm thinking about writing a function to parse a PostgreSQL timestamp.
The timestamp is in the format: Y-m-d H:i:s.u. E.g. 2011-04-08 23:00:56.544.
I'm thinking about something like this:
function parse_timestamp($timestamp, $format = 'd-m-Y')
{
// parse the timestamp
return $formatted_timestamp;
}
However I am wondering whether this can also be achieved without writing a parser for it myself (with the use of some PHP function).
function parse_timestamp($timestamp, $format = 'd-m-Y')
{
return date($format, strtotime($timestamp));
}
Don't forget to set timezone before, e.g.
date_default_timezone_set('UTC');
Or in your case, I guess 'Europe/Amsterdam'.
You can always get PHP timestamp of this format Y-m-d H:i:s.u using strtotime(). Then, using date() you can export time in your own format. Both functions depend of time zone set.
strtotime is perfectly capable of parsing that time string, then just reformat it with date:
echo date('d-m-Y', strtotime('2011-04-08 23:00:56.544')); // 08-04-2011
For those using DateTime class:
DateTime::createFromFormat('Y-m-d H:i:s.u', $yourTime);
If the database isn't giving you what you want, change it. PostgreSQL can also format dates and times.
select to_char(timestamp '2011-03-04 07:04:00', 'DD-MM-YYYY');
04-03-2011
But that's risky in international contexts, like the web. Different locales expect different ordering of elements. (Does "04-03" mean 03-April or 04-March?) This expression is easier to understand, and it uses locale-specific abbreviations for the months.
select to_char(timestamp '2011-03-04 07:04:00', 'DD-Mon-YYYY');
04-Mar-2011
Take a look at the strptime function - it can parse many time formats.