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');
Related
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);
I'm upload the excel data into mysql. in there date save like 16.5.59(dd.mm.yy) formate. I try to change this using this code
$date = '16.5.59';
echo date('d-m-y',strtotime($date));
it always show current time like 04-02-15.
Pls change this to dd-mm-yyyy formate.
Thanks in Advance.
Timestamp was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).
If it not cover the between given range then it will take current date.
$date = '16.5.59';
$date_array = explode(".",$date);
$var_day = $date_array[0];
$var_month = $date_array[1];
> Blockquote
$var_year = $date_array[2];
echo $new_date_format = "$var_day-$var_month-$var_year";
Try to make the date yourself using explode and strtotime:
date_default_timezone_set('America/Los_Angeles');
$date = '16.5.59';
$date_time = strtotime(implode('-', array_reverse(explode('.', $date))))
$date_str = date('d-m-Y', $date_time);
echo $date_str;
Output: 16-5-2059
You ARE required to specify that the year is 1959 instead of 2059 (or are you sure that it's really 2059?)
You can make change from 2059 to 1959 like this:
date_default_timezone_set('America/Los_Angeles');
$date = '16.5.59';
$date_arr = array_reverse(explode('.', $date))
# This line does the job. Make sure all years are between 1900 - 1999.
$date_arr[0] = '19' . $date_arr[0];
$date_time = strtotime(implode('-', $date_arr))
$date_str = date('d-m-Y', $date_time);
echo $date_str;
I hope this answer can help you.
This is by no means the best solution but using php's date_parse_from_format is possible. You could supply a format and use the following
$date = "16.5.59";
$dateObj = date_parse_from_format("j.n.yy", $date)
with the $dateObj you can work what you need such as :
echo $dateObj['year'];
IDE Running Example
Worth noting you require PHP >=v5.3
You can do this if it is for simpler task. It will consume time.
$date = '16.5.59';
$dtSplit=explode(".",$date);
echo "<br>".$dtSplit[0].".".$dtSplit[1].".".$dtSplit[2];
echo "<br>".$dtSplit[0].".".$dtSplit[2].".".$dtSplit[1];
echo "<br>".$dtSplit[1].".".$dtSplit[0].".".$dtSplit[2];
echo "<br>".$dtSplit[1].".".$dtSplit[2].".".$dtSplit[0];
echo "<br>".$dtSplit[2].".".$dtSplit[0].".".$dtSplit[1];
echo "<br>".$dtSplit[2].".".$dtSplit[1].".".$dtSplit[0];
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.
I'm having date 20/12/2001 in this formate . i need to convert in following format 2001/12/20 using php .
$var = explode('/',$date);
$var = array_reverse($var);
$final = implode('/',$var);
Your safest bet
<?php
$input = '20/12/2001';
list($day, $month, $year) = explode('/',$input);
$output= "$year/$month/$day";
echo $output."\n";
Add validation as needed/desired. You input date isn't a known valid date format, so strToTime won't work.
Alternately, you could use mktime to create a date once you had the day, month, and year, and then use date to format it.
If you're getting the date string from somewhere else (as opposed to generating it yourself) and need to reformat it:
$date = '20/12/2001';
preg_replace('!(\d+)/(\d+)/(\d+)!', '$3/$2/$1', $date);
If you need the date for other purposes and are running PHP >= 5.3.0:
$when = DateTime::createFromFormat('d/m/Y', $date);
$when->format('Y/m/d');
// $when can be used for all sorts of things
You will need to manually parse it.
Split/explode text on "/".
Check you have three elements.
Do other basic checks that you have day in [0], month in [1] and year in [2] (that mostly means checking they're numbers and int he correct range)
Put them together again.
$today = date("Y/m/d");
I believe that should work... Someone correct me if I am wrong.
You can use sscanf in order to parse and reorder the parts of the date:
$theDate = '20/12/2001';
$newDate = join(sscanf($theDate, '%3$2s/%2$2s/%1$4s'), '/');
assert($newDate == '2001/12/20');
Or, if you are using PHP 5.3, you can use the DateTime object to do the converting:
$theDate = '20/12/2001';
$date = DateTime::createFromFormat('d/m/Y', $theDate);
$newDate = $date->format('Y/m/d');
assert($newDate == '2001/12/20');
$date = Date::CreateFromFormat('20/12/2001', 'd/m/Y');
$newdate = $date->format('Y/m/d');
$doba = explode("/", $dob);
$date = date("Y-m-d", mktime(0,0,0, $doba[0], $doba[1], $doba[2]));
The above code turns any date i pass through into 1999-11-30 and i know it was working yesterday. Date is correct when I echo $doba. Anyone have any ideas?
Cheers
What is the format of $doba? Remember mktime's syntax goes hour, minute, second, month, day year which can be confusing.
Here's some examples:
$doba = explode('/', '1991/08/03');
echo(date('Y-m-d', mktime(0,0,0, $doba[1], $doba[2], $doba[0]);
$doba = explode('/', '03/08/1991');
echo(date('Y-m-d', mktime(0,0,0, $doba[1], $doba[0], $doba[2]);
or even easier: $date = date('Y-m-d', strtotime($dob))
It is a bit overkill to use mktime in this case. Assuming $dob is in the following format:
MM/DD/YYYY
you could just to the following to acheive the same result (assuming $dob is always valid):
$doba = explode("/", $dob);
$date = vsprintf('%3$04d-%1$02d-%2$02d', $doba);
If you have issues with what jcoby said above, the strptime() command gives you more control by allowing you to specify the format as well.