Split string from a date - php

I want split a string using PHP to get a individual date info.
For example:
$date = '08/05/2010';
would create the varables
$month = '08';
$day = '05';
$year = '2010';
Thank you.

Use explode:
$date = '08/05/2010';
list($month, $day, $year) = explode('/', $date);

if that's your example, you could explode it into an array.
$array = explode('/', $date);

list($month, $day, $year) = explode('/', $date);

Assuming it's always in that format, you want explode:
<?php
$date = '08/05/2010';
$arr = explode("/", $date);
list($month, $day, $year) = $arr;
// $month = 08, $day = 05, $year = 2010.
?>

The answers posted above would do the trick. You probably also want to check the date conforms to your expected format before you run your function. The checkdate function would be useful for this, or this snippet is a standalone implementation.

I agree with the comments about using the checkdate function, however that is really for after you split the date apart because you pass in each part (month, day, year) to check.
If you are getting the original date from user input you might want to make sure that you are getting the right format before you separate it out (using list or explode as shown previously.)
I see two options for validity if it is coming from a user: first is to only allow them to select the date, and not freely enter it. The second is to make sure you are getting it in the mm/dd/yyyy format you are expecting. You could perform a regular expression match on it before the separation. The regular expression could be something like /\d{2}/\d{2}/\d{4}/
Anyway, not really needed if you are sure of your source, but it is always important to think of where your data is coming from and how clean it is.

Related

How to simply explode (d.m.Y H:i) using Php

I will use the easiest way to explode (day,month,year,hour,minute) from a date string like this one "2014-08-30T15:30:00". My Php code does not work, because my result is always "01.01.1970 01:33".
Code:
$day = date("d",$timestamp);
$month = date("m",$timestamp);
$year = date("Y",$timestamp);
$hour = date("H",$timestamp);
$minute = date("i",$timestamp);
Straight from php's documentation: link
<?php
print_r(date_parse("2006-12-12 10:00:00.5"));
?>
This will give you an array with the different parts of the date, as you asked for ("easiest way to explode").
Use strtotime(string) function, for example date('d', strtotime('2014-08-30'))

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.

Re-Format Date with PHP

I have the following value in a variable: 26/03/2011
I'd like to get this in the format: 2011-03-26
How do I achieve this?
Many thanks
Try this:
list($d, $m, $y) = explode("/", "26/03/2011");
echo "$y-$m-$d";
While there are many ways to do this, I think the easiest to understand and apply to all date conversions is:
$date = date_create_from_format('d/n/Y', $date)->format('Y-n-d');
It is explicit and you'll never have to wonder about m/d or d/m, etc.
Might be a good idea to use the date() function combined with mktime():
$date = explode('/', '26/03/2011');
echo date('Y-m-d', mktime(0,0,0,$date[1],$date[0],$date[2]));
The reason why this could be a good idea is if you ever want to format the date in a different (more complex) format, say "March 26th, 2011" you could just do:
$date = explode('/', '26/03/2011');
echo date('F jS, Y', mktime(0,0,0,$date[1],$date[0],$date[2]));

change dd/mm/yy date format to yy/mm/dd using php

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

PHP mangles my dates

$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.

Categories