datetime to timestamp [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Inverse date function? - not strtotime
Is it possible to get UNIX time from such date 2011-02-27 02:04:46?
hello,
we have this function to convert a timestamp to datetime:
$datetime = date('Y-m-d H:i:s', $timestamp);
is there a function to do the opposite?
datetime to timestamp.
Thanks

$timestamp = strtotime($datetime);
Or if you're confident of the format, split up the string with explode() or even substr and pass the necessary parts into the mktime function.
Be aware that strtotime can sometimes get the timestamp wrong, if a slightly unconventional format is used.
EDIT:
A really accurate way of doing this is if you know your input format, is to use DateTime::createFromFormat eg:
$dateTimeObject = \DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');
See http://php.net/manual/en/function.date.php for formatting guides, and http://php.net/manual/en/datetime.createfromformat.php for info on the method described above.

$timestamp = strtotime('12-04-2010');

Related

What is this date format? 2021-10-04T08:19:54.000+04:00 [duplicate]

This question already has answers here:
What time stamp format is this?
(2 answers)
Closed 1 year ago.
And how can I covert it to 'd.m.Y H:i:s' with php?
gmdate('d.m.Y H:i:s', '2021-10-04T08:19:54.000+04:00')
did not help
The date format is ISO8601 if I'm not mistaken. PHP can parse this using the default DateTime class:
$date = new DateTime('2021-10-04T08:19:54.000+04:00');
$date->format('d.m.Y H:i:s');
For this purpose just use the native DateTime class. It can interpret several formats. Yours looks like ISO8601.
echo (new DateTime('2021-10-04T08:19:54.000+04:00'))->format('d.m.Y H:i:s');
its ISO 8601 date and you can format this easyly with dateTime or carbon.
You can find more info here https://www.php.net/manual/en/datetime.format.php

String to Date or Date and Time [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a string like this "20180720171534449" which is a kind of time stamp, is there an easy way I can convert this using PHP and format it as a date or date and time that makes sense to a human?
TIA
Peter
You have an 'YmdHisv' format where v is miliseconds.
Miliseconds is not parsable (as I found out today) with date_create_from_format so you need to remove that first from the string with substr.
$s = "20180720171534449";
$date = date_create_from_format('YmdHis', substr($s,0,-3));
echo date_format($date, 'Y-m-d H:i:s'); //2018-07-20 17:15:34
https://3v4l.org/m1XNd
As Ghost pointed out milliseconds is parasble if using microseconds u instead.
$s = "20180720171534449";
$date = date_create_from_format('YmdHisu', $s);
echo date_format($date, 'Y-m-d H:i:s\.v'); //2018-07-20 17:15:34.449

Convert DATETIME issue [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I've had a very good search on this topic but haven't managed to turn up a definitive answer that gives me what i need. What i'm trying to do is convert as follows (in PHP):
From: '2014-04-16 08:22:00.000'
To: '16/04/2014 08:22'
And then back again, does anyone have an idea as to how this might be achieved? I not using seconds so i don't need that portion of the format only d/m/Y m:i. The original format comes from an MSSQL DB datetime field and the converted format will show in a input field.
Many thanks in advance.
DateTime::createFromFormat is your friend.
$date = DateTime::createFromFormat('Y-m-d H:i:s.u', '2014-04-16 08:22:00.000');
echo $date->format('d/m/Y H:i');
OR
$date1 = new DateTime('2014-04-16 08:22:00.000');
echo $date1->format('d/m/Y H:i');
From DATETIME to PHP Date Formting:
date("WHATEVER", strtotime($mysql_result->date))
To DATETIME from date:
date('Y-m-d H:i:s', strtotime($time))
https://php.net/manual/en/function.date.php
https://php.net/manual/en/function.strtotime.php
Try to find your answer on google before:
http://www.php.net/manual/en/datetime.formats.date.php
or
Convert one date format into another in PHP

Date format conversion results wrong date [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I tried converting
12-18-1997
to
18-12-1997
with this code
$new_date = date('d-m-Y', strtotime('12-18-1997'));
but it results in 18-12-1969
If I have to convert full date alongwith time then its converting fine but in the date I posted in question there is no time.
Use DateTime instead of strtotime():
$date = DateTime::createFromFormat( 'm-d-Y', '12-18-1997');
echo $date->format( 'd-m-Y');
You can see from this demo that it prints:
18-12-1997
strtotime is good, but it's not psychic or omniscient. you're feeding it a time string it's not able to parse properly:
php > var_dump(strtotime('12-18-1997'));
bool(false)
Since you simply assumed it's succeeding, you feed that false back to date(), where it's type-cast to an integer 0. However, your result is impossible, since int 0 as a date is Jan 1/1970. With timezone conversions, it'd be 31-12-1969 for you, NOT 18-12.
If you can't feed strtotime a format it understands, then use date_create_from_format and TELL it what what the format is:
$date = date_create_from_format('m-d-Y', '12-18-1997');
$text = date('d-m-Y', $date);

How can i take sql current timestamp and output as php date()? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Formating an SQL timestamp with PHP
I have a column in my SQL table that has default current timestamp values. I am trying to output it in a more readable format using date().
However, no matter which format I use, I always get the same date: 1970-01-01 00:33:32
Here is an example value of the current timestamp from the DB: 2012-08-09 06:37:58
Below is the code I use to try to "convert" it to a readable format:
$value['current_date']// is the var from the database.(containing 2012-08-09 06:37:58)
$somevar = date('Y-m-d H:i:s', $value['current_date']);
Thanks in advance.
Your problem is that you are using the wrong value in the second parameter. The date() function expects a UNIX-style timestamp as the second parameter, not a string representation of a date. Use the strtotime() function to correct this:
$somevar = date('Y-m-d H:i:s', strtotime($value['current_date']));
As someone else pointed out, however, why are you bothering to do this formatting? The format style you want is already in the database. It should be as easy as:
echo $value['current_date'];
The problem that you are having is because the second argument of the date() should be a timestamp, and since you are passing the raw string containing 2012-08-09 06:37:58, php does not know what to make of it.
Modify your code as below and it should work:
$somevar = date('Y-m-d H:i:s', strtotime($value['current_date']));
You can now use any date formats in place of 'Y-m-d H:i:s' as you wish
First of all: Why formatting the timestamp if it already exists in the desired format?
Regarding the actual problem:
See http://de2.php.net/manual/en/function.date.php
date() only accepts unix timestamps (seconds since 1970-1-1 00:00:00 UTC) or no timestamp at all. if you want to work with a timestamp like you have you need to create an unix timestamp first with date_create(), mktime , or strtotime

Categories