I'm calling the API and from getting the Date as /Date(1576648800000)/ this format.
I don't know what the format is?
I want to convert it to Y-m-d H:i:s in php can anyone know how can I do it?
Use Regex to extract the timestamp from the string.
$re = '/\((\d+)\)/m';
$str = '/Date(1576648800000)/';
preg_match($re, $str, $matches);
$d = DateTime::createFromFormat('U', $matches[1] / 1000);
echo $d->format('Y-m-d H:i:s');
which results:
2019-12-18 06:00:00
This is timestamp with miliseconds. you can change it to what you want. first you must remove miliseconds.
$time = 1576648800000;
$time = $time / 1000;
then you can change it with date() function:
date('Y-m-d H:i:s',$time)
for get integer from string do this:
$dateString = '/Date(1576648800000)/';
$time = preg_replace('/[^0-9]/', '', $dateString );
You can use regex to filter the value. Since your format goes with /Date(some_number)/, you can simply match the digits inside the bracket.
Snippet:
<?php
$str = '/Date(1576648800000)/';
if(preg_match('/\((\d+)\)/',$str,$matches) === 1){
$date = date("Y-m-d H:i:s",intval($matches[1]) / 1000);
echo $date;
}else{
throw new Exception('Unexpected format received from the API');
}
Demo: https://3v4l.org/cOCIq
To get the time from /Date(1576648800000)/:
$string = '/Date(1576648800000)/';
$string = intval(substr($string, 6, -2)) / 1000;
date('Y-m-d H:i:s',$string);
Related
I have a string that contains date and time. Format of my string is yyyymmddtime.
For example. 20171125123000209. this is my complete string in which first comes the month, then month and then day after that time. How can i retrieve date from it by converting to readable date format. I tried with php's date function. But the output was not as expected. Please help.
Try this
<?php
$str_date= "20171125123000209";
$exiting_date_format='Ymd';
//first 8 characters from given date string in second parameter below
$date = DateTime::createFromFormat($exiting_date_format, substr($str_date,0,8));
echo $date->format('Y-m-d');//specify desired date format
?>
Output :
2017-11-25
DateTime::createFromFormat - Parses a time string according to a specified format
You can use date() function of php
<?php
$full_date= "20171125123000209";
echo date('dS F h:i:s A', $full_date);
//Output = 31st May 12:30:09 AM
?>
Or,
To get date from timestamp like now,
$timestamp= time(); //Or your timestamp here
$date = date('d-m-Y', $timestamp); //Inside first parameter, give your date format
echo $date; //17-12-2017
Or,
To get anything from string you can also use substr() function of php.
<?php
$full_date= "20171125123000209";
$year = substr($full_date, 0, 4);
$month = substr($full_date, 4, 2);
$date = substr($full_date, 6, 2);
echo 'Year = '.$year.' ';
echo 'Month = '.$month.' ';
echo 'Date = '. $date.' ';
?>
Output:
Year = 2017 Month = 11 Date = 25
Test in jdoodle
About substr() function in php
It's not necessary to manipulate the string at all. PHP's DateTime class supports parsing a string containing miliseconds natively, using the u format modifier:
$str = '20171125123000209';
$date = DateTime::createFromFormat('YmdHisu', $str);
Using your new $date object, you can convert to whatever format you're looking for, e.g.
echo $date->format("F j Y, g:i a");
// November 25 2017, 12:30 pm
See https://eval.in/920636
$your_strtotime_val = ""; //20171125123000209
$convert_to_date = date("m-d-Y h:i:s",$your_strtotime); // [m-d-Y h:i:s] this depending on how you convert date in first time so be careful
I have a problem by converting a date in proper format.
I get the time the from Facebook API in this format: 2013-08-23T09:00:00
I then $fbdate = date('2013-08-23T09:00:00');
When I echo $fbdate, it retuns 2013-08-25UTC05:00:00.
Then I tried:
$datum = date("d.m.Y",$fbdate);
$uhrzeit = date("H:i",$fbdate);
To extract the date and the time but it always returns:
01.01.1970 for $datum and 00:33 for $uhrzeit.
You should use strtotime() to parse a date string into a UNIX timestamp:
$fbdate = strtotime('2013-08-23T09:00:00');
$datum = date('d.m.Y', $fbdate);
$uhrzeit = date('H:i', $fbdate);
Try using the DateTime class:
$fbdate = '2013-08-23T09:00:00';
$date = DateTime::createFromFormat('Y-m-d\TH:i:s', $fbdate);
$datum = $date->format('d.m.Y');
$uhrzeit = $date->format('H:i');
echo $datum;
echo $uhrzeit;
$datum = date("d.m.Y",strtotime($fbdate));
$uhrzeit = date("H:i",strtotime($fbdate));
The date function in PHP is meant to convert a microtime into a date, so you need to convert your string dates to microtimes first.
Have you tried this? You should use strtotime() when using the date function.
$datum = date("d.m.Y", strtotime($fbdate));
$uhrzeit = date("H:i", strtotime($fbdate));
Use strtotime()
$a = date("Y-M-d", strtotime($datum));
echo $a.$uhrzeit;
More info http://php.net/manual/en/function.strtotime.php
I'm not a huge fan of using timestamp.
What I did to solve this issue was:
$updatedDate = new DateTime(
preg_replace('/^(.*)\+0000$/', '$1', $fbUser->getProperty("updated_time")),
new DateTimeZone("UTC")
);
It chops the +0000 part and creates the DateTime object with an explicit UTC timezone.
How to convert date from yyyy:mm:dd to yyyy-m-dd and yyyy:mm:dd (without leading zero for the month) to yyyy-m-dd (with leading zero to month)?
You could use DateTime::createFromFormat and then use DateTime::format.
Example:
$date = DateTime::createFromFormat('Y:m:d', '2012:08:02');
echo $date->format('Y-m-d');
// without leading zero for month
$date = DateTime::createFromFormat('Y:n:d', '2012:8:02');
echo $date->format('Y-m-d');
try this:
$dateFrom ="2012:8:2";
$dateTo = str_replace(":","-",$dateFrom);
$dateTo = date("Y-m-d", strtotime($dateTo));
echo $dateTo;
Use the $date=strtotime($date) function to get the date in unix timestamp. After that you can use the date("Y-m-d",$date) function to convert it to the format you want.Here's an example:
$date=strtotime($olddate);
$date=date("Y-m-d",$date);
echo $date; // Now this will show you the date in the format you wanted :)
Use the date function.
date will not understand : as a separator, so you will need to replace that with a separator that it understands, like / or -, with str_replace.
Code:
$orig_date = '2012:8:2';
$final_date = date('Y-n-d', str_replace(':', '/', $orig_date));
echo $final_date; // Result: 2012-8-02
Use date() function
echo date('Y-m-d'); // for 1st case (replacing ':' with '-')
echo date('Y-j-d'); // for 2nd case (without leading zero)
I was wondering if there is a way, using PHP, to change this date format: 01.08.86 (January 8, 1986) to this format: 1.8.86.
<?php
$date = "01.08.86";
$unix = strtotime($date);
echo date('n.j.y', $unix);
How about a regex based solution:
$str = '01.08.86';
$a = array('/^0(\d+)/','/\.0(\d+)/');
$b = array('\1','.\1');
$str = preg_replace($a,$b,$str);
// $str is now '1.8.86'
How can I convert this string 05/Feb/2010:14:00:01 to unixtime ?
Use the strtotime function:
Example:
$date = "25 december 2009";
$my_date = date('m/d/y', strtotime($date));
echo $my_date;
For PHP 5.3 this should work. You may need to fiddle with passing $dateInfo['is_dst'], wasn't working for me anyhow.
$date = '05/Feb/2010:14:00:01';
$dateInfo = date_parse_from_format('d/M/Y:H:i:s', $date);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year'],
$dateInfo['is_dst']
);
Versions prior, this should work.
$date = '05/Feb/2010:14:00:01';
$format = '#^(?P<day>\d{2})/(?P<month>[A-Z][a-z]{2})/(?P<year>\d{4}):(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})$#';
preg_match($format, $date, $dateInfo);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
date('n', strtotime($dateInfo['month'])), $dateInfo['day'], $dateInfo['year'],
date('I')
);
You may not like regular expressions. You could annotate it, of course, but not everyone likes that either. So, this is an alternative.
$day = $date[0].$date[1];
$month = date('n', strtotime($date[3].$date[4].$date[5]));
$year = $date[7].$date[8].$date[9].$date[10];
$hour = $date[12].$date[13];
$minute = $date[15].$date[16];
$second = $date[18].$date[19];
Or substr, or explode, whatever you wish to parse that string.
You should look into the strtotime() function.
http://www.php.net/date_parse_from_format
$d="05/Feb/2010:14:00:01";
$dr= date_create_from_format('d/M/Y:H:i:s', $d);
echo $dr->format('Y-m-d H:i:s');
here you get date string, give format specifier in ->format() according to format needed
Simple exploding should do the trick:
$monthNamesToInt = array('Jan'=>1,'Feb'=>2, 'Mar'=>3 /*, [...]*/ );
$datetime = '05/Feb/2010:14:00:01';
list($date,$hour,$minute,$second) = explode(':',$datetime);
list($day,$month,$year) = explode('/',$date);
$unixtime = mktime((int)$hour, (int)$minute, (int)$second, $monthNamesToInt[$month], (int)$day, (int)$year);
If you're up for it, use the DateTime class
Try this:
$new_date=date('d-m-Y', strtotime($date));
If it's a string that you trust meaning that you have checked it before hand then the following would also work.
$date = new DateTime('2015-03-27');