how to display 4-1 as month and date in php - php

I Am developing a php web application. Here I have date in a variable in the format 'm/d'
$var='04/25'
I have stored this value as strtotime with in my table
$var2=strtotime($var);
Then I need to display this value as 4-25 i.e. remove 0 from 04.
If the date is 1 I need to display 1 not 01
Does anyone know this?

Use the PHP date function. In your case I would say date("n-j", $var2);

Too many ways to do this, so let's go with one of them:
ltrim( $var, '0' );

I always store timestamps, that way they can easily be used with date() function to convert them to any format I need.
$now = time();
print date("n-d",$now);

Just use date function: http://php.net/manual/en/function.date.php
echo date('n-j', $var2);

try this :
copy below code and past in test.php and after run you get the out put : 4-25
<?php
$date = "04/25";
$temp = strtotime($date);
echo date('n-j',$temp);
?>

Related

Substruct days stored in variable from date in PHP

As the title says I am trying to subtract a number of days (stored in variable) from a date. I am using the date_sub function.
This is the variable which contains the days that I want to substract (integer from a field)
$days=$rowRate['days'];
The function:
$chargedate=date_sub($arrivaldate,date_interval_create_from_date_string($days."days"));
$arrivaldate is the variable that contains the date that i want to substract the $days from.
When I use it, I get the warning date_sub() expects parameter 1 to be DateTime, string given in :\xampp\htdocs\Project\file.php on line 55.
I think I am messing up with the concatenation but I can not figure this out. Any help would be appreciated.
Try converting the $arrivaldate variable like this before passing it into the date_sub function:
$arrivaldate = date('Y-M-D', strtotime($arrivaldate));
$chargedate=date_sub(
$arrivaldate,
date_interval_create_from_date_string($days."days")
);
Per the error message you need to create a DateTime object from the $arrivaldate before calling date_sub:
$date = date_create(date('Y-m-d', strtotime($arrivaldate)));
$chargedate = date_sub($date,date_interval_create_from_date_string($days." days"));
echo date_format($chargedate, "Y-m-d");
I added the strtotime because it's not clear what format you are using but it may not be necessary.
The date_sub and date_add functions are only used in practice if a DateInteval is already available. Your task can be solved more efficiently and easily with DateTime::modify().
$arrivaldate = "2021-08-16";
$days = "5"; //may be also integer
$startDate = date_create($arrivaldate)->modify(-$days."Days");
//test output: 11/08/2021
echo $startDate->format("d/m/Y");
Important: The date in $arrivaldate must have a format wich accepted from DateTime.

i want month name from date function using php

date_default_timezone_set('Asia/Calcutta');
$date=date("d:m:Y");
Now `$date=14072015;
I want month name 'July' from this date function. i have tried this,but i got output January.
$arr1=explode(":",$date);
$arr2=implode($arr1);
// print_r($arr2);die;
// print_r($arr1);die;
// print_r($date);DIE;
$year=date("Y");
$month=date("M",strtotime($arr2));
print_r($month);DIE;
I Want OUTPUT July.Please help.if anyother methods to solve this please help.
string date ( string $format [, int $timestamp = time() ] )
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
Read php.net manual
Try
<?php echo date('F', time());?>
In your question you are using $month=date("M",strtotime($arr2));.
Use time($arr2) instead of strtotime($arr2)
DEMO
"F" returns the full name of month
<?php echo date('F'); ?>
You could use date function directly in php
try something like this
echo date("F");
U can Try this below function
date('F');

break d date from yyyy-mm-dd into mm,yyyy and dd as separate elements

I am storing the date in MySQL in the format yyyy-mm-dd. I wish to extract individual elements of the date i.e. day,month and year as separate elements
How should I go about this?
I am using php
You can do it using explode function in php.
explode('-', $date_obj);
You could make use of the DateTime class which has the createFromFormat.
Like this..
<?php
$date = DateTime::createFromFormat('Y-m-d', '2009-02-23');
echo $date->format('Y');//2009
echo $date->format('F');//February or use 'M' for Feb
echo $date->format('d');//23

strtotime() converts a non existing date to another date

I am building a timestamp from the date, month and year values entered by users.
Suppose that the user inputs some wrong values and the date is "31-02-2012" which does not exist, then I have to get a false return. But here its converting it to another date nearby. Precisely to: "02-03-2012"..
I dont want this to happen..
$str = "31-02-2012";
echo date("d-m-Y",strtotime($str)); // Outputs 02-03-2012
Can anyone help? I dont want a timestamp to be returned if the date is not original.
You might look into checkdate.
That's because strtotime() has troubles with - since they are used to denote phrase like -1 week, etc...
Try
$str = '31-02-2012';
echo date('d-m-Y', strtotime(str_replace('-', '/', $str)));
However 31-02-2012 is not a valid English format, it should be 02-31-2012.
If you have PHP >= 5.3, you can use createFromFormat:
$str = '31-02-2012';
$d = DateTime::createFromFormat('d-m-Y', $str);
echo $d->format('d-m-Y');
You'll have to check if the date is possible before using strtotime. Strtotime will convert it to unix date meaning it will use seconds since... This means it will always be a date.
You can workaround this behavior
<?php
$str = "31-02-2012";
$unix = strtotime($str);
echo date('d-m-Y', $unix);
if (date('d-m-Y', $unix) != $str){
echo "wrong";
}
else{
echo date("d-m-Y", $unx);
}
or just use checkdate()
Use the checkdate function.
$str = "31-02-2012";
$years = explode("-", $str);
$valid_date = checkdate($years[1], $years[0], $years[2]);
Checkdate Function - PHP Manual & Explode Function - PHP Manual
Combine date_parse and checkdate to check if it's a valid time.
<?php
date_default_timezone_set('America/Chicago');
function is_valid_date($str) {
$date = date_parse($str);
return checkdate($date['month'], $date['day'], $date['year']);
}
print is_valid_date('31-02-2012') ? 'Yes' : 'No';
print "\n";
print is_valid_date('28-02-2012') ? 'Yes' : 'No';
print "\n";
Even though that date format is acceptable according to PHP date formats, it may still cause issues for date parsers because it's easy to confuse the month and day. For example, 02-03-2012, it's hard to tell if 02 is the month or the day. It's better to use the other more specific date parser examples here to first parse the date then check it with checkdate.

Reformat Custom Date in PHP

So I know how to format a date in PHP, but not from a custom format. I have a date that is a string "YYMMDD" and I want to make it "MMDDYYYY'. strtotime doesn't seem like it would do a good job of this when the MM and DD are both low digits.
Use str_split:
$date1 = "YYMMDD";
list($yy, $mm, $dd) = str_split($date1, 2);
// MMDDYYYY format, assuming they are all > 2000
$date2 = $mm . $dd . "20" . $yy;
If you're running PHP >= 5.3, have a look at DateTime::createFromFormat. Otherwise, if you don't want to use pure string manipulation techniques, use the more primitive strptime together with mktime to parse the time into a UNIX timestamp, which you can then format using date.
Maybe I am under-thinking this, but couldn't you just:
$oldDate='040220'; // February 20th, 2004
$year = substr($oldDate, 0,2);
$year += $year &lt 50 ? 2000 : 1900;
$date = preg_replace('/\d{2}(\d{2})(\d{2})/', '$1/$3/'.$year, $oldDate);
And you'd have the string you were looking for, or something close enough to it that you could modify from what I wrote here.
Have many dates prior to 1910? If not, you could check your YY for <=10, and if true, prepend "20" else prepend "19"... Kinda similar approach to MM and DD check for <10 and prepend a "0" if true... (This is all after exploding, or substring... Assign each part to its own variable, i.e. $M=$MM; $D=$DD; $Y=$YYYY; then concatenate/arrange in whatever order you want... Just another potential way to skin the proverbial cat...
Ended up doing:
$expiration_date_year = substr($matches['expiration_date'],0,2);
$expiration_date_month = substr($matches['expiration_date'],2,2);
$expiration_date_day = substr($matches['expiration_date'],4,2);
$expiration_date = date('m/d/Y', mktime(0,0,0,$expiration_date_month, $expiration_date_day, $expiration_date_year));

Categories