This question already has answers here:
Convert timestamp to readable date/time PHP
(14 answers)
Closed 8 years ago.
simple question, I have a date, I think it a Timestamp, I'm not sure but it looks something like that:
'/Date(1403024400000)/'
How do I convert that to D/M/Y Just a regular Europe date.
Basically you need to extract the timestamp from the string first. This can be done using a regex.
The timestamp is a UNIX timestamp with millisecond accuracy. Therefore you need to remove the last 3 digits from it before you can convert it to the desired format.
Like this:
$string = '/Date(1403024400000)/';
// retrieve the timestamp with a regex
preg_match('/Date\((.*?)\)/', $string, $matches);
$timestamp = $matches[1];
// the timestamp contains milliseconds. remove the last 3 digits
$timestamp = substr($timestamp, 0, -3);
// convert to d/m/Y using date()
echo date('d/m/Y', $timestamp);
~
Output:
17/06/2014
Btw, there is no "regular" European date format. In germany for example we are using d.m.Y (using dots as separator)
Chop off the last three numbers (as it doesn't seem you care about that anyway) and do something like this
echo date('d/m/Y', 1403024400);
Related
This question already has answers here:
Getting date format m-d-Y H:i:s.u from milliseconds
(18 answers)
Closed 2 years ago.
How to produce this kind of date string in PHP ?
this sample date string below is coming from a postgresql output
2020-10-20 14:44:37.060966+08
I already tried something like
date('Y-m-d H:i:sO',strtotime('now'));
and
date('Y-m-d H:i:sOT',strtotime('now'));
but still i cannot produce the part starting from the decimal point to the end of my sample string.. so how to generate that format with decimal and + something something right after the seconds in PHP ?
That part is microsecond, you can use this format Y-m-d H:i:s.uO to add microsecond.
One thing to note that microsecond format with date() will always be 000000. Use DateTime::format() instead:
$date = new DateTime();
echo $date->format('Y-m-d H:i:s.uO');
Output:
2020-12-07 03:17:37.011493+0100
This question already has answers here:
Converting string to Date and DateTime
(13 answers)
Convert one date format into another in PHP
(17 answers)
Closed 3 years ago.
I'm trying to convert a string of numbers I am pulling from a db that looks like this '010219', representing January 2, 2019. I cannot find a way to convert this into 2019-01-02 using php, I just keep getting today's date from the functions I am trying.
Needs to be accomplished with no separators in original string.
There are a variety of ways to accomplish this, however the most concise is probably to use date_create_from_format.
An example is here:
$date = date_create_from_format('dmy', '010219');
This will output a Date as so:
echo date_format($date, 'Y-m-d');
Outputs: 2019-02-01
The date_create_from_format function accepts a parameter that defines the format of the date. In this case, the format is dmy which means:
d - Day of month as two-digit number (01-31)
m - Month of year as two-digit number (01-12)
y - Year as two-digit number
The documentation for date_create_from_format is here.
have you tried something like this?
<?php
$str="010219";
$date = DateTime::createFromFormat('dmy', $str);
echo $date->format('Y-m-d'); //2019-02-01
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2003-10-16
Please see source: Converting string to Date and DateTime
split and concatenate with preg_replace
$newformat = preg_replace("/^(\d\d)(\d\d)(\d\d)$/","20$3-$1-$2","010219");
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I'm writing some code a script, which has short dates like
14/12/17
30/11/17
20/11/17
I need to convert these to long date format so i used PHP date function and strtotime as below
echo date('d-m-Y',strtotime('14/12/17'));
But it always getting 01-01-1970 as output, but it should be 14-12-2017
anyone know how to convert this to a long date format please.
PS. Other question answers suggest change the date input format, but I cannot change date input since it's getting from another site
This is from the link I posted and OP says is not correct.
Originally posted by ceiroa.
Convert one date format into another in PHP
$myDateTime = DateTime::createFromFormat('d/m/y','14/12/17');
$newDateString = $myDateTime->format('d-m-Y');
Echo $newDateString;
https://3v4l.org/2AcAd
Use preg_replace to flip the day and month on the incoming dates, then use date as you want.
$orig = ['14/12/17', '30/11/17', '20/11/17'];
foreach ($orig as $s) {
echo date('d-m-Y', strtotime(preg_replace('#(\d+)\/(\d+)\/(\d+)#', '$2/$1/$3', $s)))."\n";
}
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
hi have my : regional format date like : 01-Iulie-2014 or 01-Decembrie-2015 and I want to convert it into: 2014-07-01 or 2015-12-01. I tried something like this,but with no result:
$mydate = date('Y-m-d', strtotime($this->input->post('pay_date')));
but strtotime returns me false
Is there a function to do this stuff ?
strtotime() parses about any English textual datetime description into a Unix timestamp, so you could do, create an array of month names in your language and its equivalent english month name, and then use strtotime, as:
function custom_strtotime($your_date) {
//create month names as your_lang_month_name => english_month_name
$months_arr = array('Janvier'=>'jan',...,'Decembrie'=>'dec')
return strtotime(strtr(strtolower($your_date), $months_arr));
}
echo custom_strtotime($some_date);
From http://php.net/manual/en/function.date.php:
To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a string like-
$strTime = "0920"
How to convert the above string to time format in PHP-
0920 -> 09:20
I am super new to PHP so may be this is a very basic question, I tried with strtotime method but it didn't give me the expected output.
You can use this fastest solution:
$strTime = "0920";
echo $strTime[0] . $strTime[1] . ':' . $strTime[2] . $strTime[3];
If you know that the format will always be the same you can do it literally:
I found an example here: Split string into 2 pieces by length using PHP
$a = ":"
$first400 = substr($str, 0, 2);
$theRest = substr($str, 2);
Then create the new time string:
echo "$first400{$a}$theRest"
I used basic string mania from here: http://www.php.net/manual/en/language.operators.string.php
You could do some basic checks to make sure it's formatted correctly. Ex if it is 4 chars in length.
strtotime() gives you the number of seconds since January 1 1970. 920 seconds from that cold, dark birth of time is not the specific time you're looking for.
If you really want that string as your output, with the colon in the middle, I would use just inject the colon into the string with substr_replace()