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'))
Related
I have this php code that I want to output the current year, month and day.
date_default_timezone_set('Sweden/Stockholm');
$time_info = getdate();
$day = $time_info['mday'];
$month = $time_info["mon"];
$year = $time_info['year'];
$date = "$year, $month, $day";
And the output:
2014 10 29
But I want it to output
year-month-day
But when I change the $date to $date = $year."-".$month."-".$day."-"; the output is: 1975.
Obviously, this is wrong, so how can I fix it. And a explanation why this occurs would also be great.
EDIT:
Ok, so according to #Marc B and #Barry , it did at some point math, and they were right. I don't know why this occurs, but I got it sorted out. Thanks!
Why don't you use the php date function?
echo date("Y-m-d");
You can get that if you run this.
$time_info = getdate();
echo $time_info->format('Y-m-d');
Using PHP format() function resolves your problem.
Documentation is here.
use php date function Use Like this
date_default_timezone_set('Sweden/Stockholm');
echo date("Y-m-d");
I would like to separately strip the day and month values out of this timestamp "2012-07-12 17:50:00".
So essentially I could have two variables like:
$month = 7
$day = 12
As I am not very proficient with php, specifically regex coding, I thought I would post this question to ask for advice as to how I would go about accomplishing something like this.
Any assistance, insight or input in this regard would be greatly appreciated. Thank you!
AS A NOTE: Futhermore, I would like to turn the two variables into an output like "12th July". This can be coded quite easily so I have not made this a part of the question but if there is a simple function that deals with this information would be much appreciated too!
As said in the comments, DateTime would be a good choice.
First, if your php config dosen't already set it up for you, set your default timezone by:
date_default_timezone_set('XXXX');
XXXX stand for a value out of the List of supported timezones
initialize your date by:
$DateTime = new DateTime();
depending on where you get the timestamp, lets assume you will create it out of PHP:
$timestamp = $DateTime->getTimestamp();
Now format the output
echo $month = $DateTime->format( 'm' );
echo $day = $DateTime->format( 'd' );
or to get you desired output:
echo $output = $DateTime->format( 'dS F' );
<?php
list($month,$day) = explode(':',date('n:j',strtotime('2012-07-12 17:50:00')),2);
echo 'Month: '.$month.'<br />'."\n";//Month: 7
echo 'Day: '.$day.'<br />'."\n";//Day: 12
?>
And...
<?php
echo date('jS F',strtotime('2012-07-12 17:50:00'));//Outputs: 12th July
?>
Assuming the timestamp is a string, something like this should work if you're looking for a regex solution...
<?php
$string = '2012-07-12 17:50:00';
preg_match_all('/\d{4}-(\d{2})-(\d{2})/', $string, $matches);
$month =$matches[1][0];
$day = $matches[2][0];
?>
I have this code:
$x = date("Y-m-d", strtotime($post['commissionEligibilityDate'] . "+ " . $post['billingPeriodExpiration'] . " months"))
$post['commissionEligibilityDate'] = 2011-11-08 <br/>
$post['billingPeriodExpiration'] = 2 <br/>
so $x returns 2012-01-08.
I have another variable $singleDate and it's equal to 1. What I am trying to do replace the 08 with 01. How can I do that?
You don't have to use Y-m-d, you can use Y-m-01 or your variable:
$x = date("Y-m-".$singleDate, strtotime($post['commissionEligibilityDate'] . "+ " . $post['billingPeriodExpiration'] . " months"))
You could use the DateTime class:
$d = new DateTime($x);
$year = $d->format('Y');
$month = $d->format('m');
$d->setDate($year, $month, '01');
echo $d->format('Y-m-d');
str_replace('08','01',$post['commissionEligibilityDate']);
$explode = explode("-",$post['commissionEligibilityDate']);
$explode[2] = $singleDate;
$post['commissionEligibilityDate'] = implode("-",$explode);
$post['commissionEligibilityDate'] will now echo 2011-11-1
Almost anything is possible with PHP. My suggestion seeing as your looking to only get one number from a date that you consider checking out http://php.net/manual/en/function.date.php to see the various ways of handling the date() output. As you could easily output a day then add to it $z = date('d', time())+1; for example.
I am not sure what your doing with your dates specifically but to me it sounds like you might have a misconception of what they are, and how to work with them. Basically the short idea of it is, a date defined in a variable is a string. You can make them anyway you want even without the use of date() then store them, as long as they are in the right format when you go to store them ie yyyy-mm-dd you should be fine.
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');
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.