Remove leading zeros from abbreviated date with PHP - php

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'

Related

Add a space in a php string at an exact space

I have lots of date and time data which have been put together like so 05/12/2113:30
What I want to do is separate into two strings like so $date = '05/12/21' and $time = 13:30 so that I can prepare them for database entry in a correct format.
They are always the same first 8 digits (including 2 '/') are the date and the last 5 digits (including ':') are the time.
How can i go about separating them correctly using php?
Thanks so much for your help and I am sure its easy I just seem to be having a brain fart moment.
$value = "05/12/2113:30";
var_dump(substr($value, 0, 8)); //05/12/21
var_dump(substr($value, 8, 5)); //13:30
Using substr() you can extract from any place.
$string = '05/12/2113:30';
$date = substr($string, 0, 8); // 05/12/21 <-- from start
$time = substr($string, -5); // 13:30 <-- from end
You can use substr:
first substring will be date (from 0 to 8)
second substring will be time (from 8 to the end of string)
$date = substr($str,0,8);
$time = substr($str,8);
Using the builtin DateTime Object its actually quite easy when you can guarantee the input format.
$in = '05/12/2113:30';
$dt = (new DateTime())->CreateFromFormat('d/m/yG:i', $in);
echo 'database format = ' . $dt->format('Y-m-d H:i:s');
RESULT
datebase format = 2021-12-05 13:30:00

Format date in php from unknown format /Date(1576648800000)/

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);

Convert date format in PHP (without SQL)

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)

How to convert date from one format to another?

I'm having the date in this format in a Excel worksheet 03-Dec-10. So its not compatible while inserting it into the database. How to convert the date to the acceptable format?
$input = '03-Dec-10';
$date = DateTime::createFromFormat('d-M-y', $input);
echo $date->format('Ymd'); // or possibly 'Y-m-d'
This will output 20101203, which is presumably what you want. If it's not exactly what you are after, have a look here.
You can also do the reverse:
$input = '20101203';
$date = DateTime::createFromFormat('Ymd', $input);
echo $date->format('d-M-y');
While Jon's answer is correct, here is another option:
$input = '03-Dec-10';
$date = date('Ymd', strtotime($input));
For a more general approach, you can always dump your current format to a string, like how you have it, and use string operations to substring and reorganize. I know for a fact that MySQL accepts string values for DATETIME fields.
$day = substr($input, 0, 2);
$month = substr($input, 2, 3);
switch($month){
case "Jan":
$month = "01";
break;
...
}
If you're doing this from Excel itself, you can put this formula into another column
=TEXT(A2, "YYYYmmdd")
Then copy down. This produces a compatible 8-digit date.

Convert String To date in PHP

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');

Categories