Echo date/month from database and echo current year - php

I would like my output to be - 05/12/2016 <--- year depends on the current year.
$date='05/12/2014';
$current_year='date("Y")';
I've tried combine like below but clearly its wrong. Please help me, how do I combine these two to get my desire output?
$date =(date("d/m", strtotime($date'])) / date("Y"));

$date='05/12/2014';
echo date("d/m", strtotime($date)) ."/". date("Y");

You should try something like this. Concate your / and remove ] .You need to format your database date.It should work.Try:
$databaseDate = date("d/m/Y", strtotime("31/12/2014")); //you need to format your databse Date
$mergeDate = date("d/m", strtotime($databaseTime)) . '/' . date("Y");
echo $mergeDate;
LIVE DEMO

Your approach can work. Just adapt it a bit:
date("d/m", strtotime($date)) . '/' . date("Y");
EDIT:
As pointed out by others, with this type of date format (d/m/Y) you might have issues when date is '25/12/2014'. To be sure your code works in any case use date_parse_from_format to control your date format:
$date = '25/12/2014';
$date_array = date_parse_from_format('d/m/Y',$date);
echo $date_array['day'] . '/' . $date_array['month'] . '/' . date("Y");

Related

PHP - Getting next thursday's date

Code
$order_dispatch_date = DateTime::createFromFormat('Ymd', $inc['order_dispatch_date']);
$order_dispatch_date = $order_dispatch_date->format('Ymd');
$order_collection = date('Ymd', strtotime('next thursday', $order_dispatch_date));
$order_collection = new DateTime($order_collection);
echo '['.$order_collection->format('d/m/Y') . ' - ' . $order_dispatch_date . ' - ' . gettype($order_dispatch_date) . ' ] ';
I get the following output if the input is: 20171128
[29/08/1970 - 20171128 - string ]
My question is why is the first output showing 29th August 1970 rather than 20th November 2017?
Stop changing things back-and-forth between objects, strings and timestamps so much, especially when you're re-using the same variables. It's needlessly hard to follow.
If you've got a Ymd formatted string, and you want an equivalent string back for the proceeding Thursday, just use this:
$inc['order_dispatch_date'] = '20171128';
$order_dispatch_date = DateTime::createFromFormat('Ymd', $inc['order_dispatch_date'])->modify('next thursday');
echo $order_dispatch_date->format('Ymd');
// 20171130
See https://eval.in/909998
Basically:
$order_collection = date('Ymd', strtotime('next thursday', strtotime($order_dispatch_date)));
And that should work :)

Date time convert is incorrectly being modified

I am having issues getting my code to return the correct response.
$Birthd = '06-27-1996';
$NewISSdate = date("m-d-Y", strtotime(date("m-d-Y", strtotime($Birthd)) . " +21 years"));
When I run this the response is: "12-31-1969"
I believe this is a default date of sorts, but what can I do to repair my code? If ran with a different $Birthd string such as "07-03-1996".
You need to change string date format and then add years to it like below:-
$Birthd = '06-27-1996';
$Birthd = str_replace("-","/",$Birthd);
echo $NewISSdate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($Birthd)) . " + 21 years"));
Output:-https://eval.in/835509 OR https://eval.in/835555
Reference:- php different acceptable date formats
Change - to / and try...
$Birthd = '06/27/1996';
$NewISSdate = date("m/d/Y", strtotime(date("m/d/Y", strtotime($Birthd)) . " +21 years"));

PHP Date incorrect with a format like YYYY/DD/MM

When I insert it into my database, it works :
$time = strtotime('1' . '/' . 10 . '/' . '2015');
$res = date('y-n-d',$time);
BUT when I insert it into my databse, it doesn't :
$time = strtotime('10' . '/' . 10 . '/' . '2015');
$res = date('y-n-d',$time);
This code insert in my database the 1970-01-01 default date...
The only change is the length of the day. I tried to change the format of the date, like Y-m-d, but it doesn't change anything.
Use DateTime interface -
$time = new DateTime('10' . '/' . 10 . '/' . '2015');
echo $res = $time->format('Y-m-d');
Use it in following way...
$time = date("d-m-Y", strtotime('1/10/2015'));
$res = date("Y-m-d", $time);
OR
$date = date_create('1/10/2015');
$res = date_format($date, 'Y-m-d');
It looks like you were trying to insert a timestamp (integer) into a DATE field. If your db is MySQL and your using DATETIME type, then you could use this php date format:
date("Y-m-d H:i:s")
Also, I'd suggest using new DateTime() as #sgtBose mentioned, as you might get unexpected results from strtotime() in different locales.
Try this:
$unixDate='01/01/1900';$differenceValue = strtotime($time)- strtotime($unixDate);
insert $differenceValue to Database.

Add 1 year to a date

I have a date 01/31/2014, and I need to add a year to it and make it 01/31/2015. I am using
$xdate1 = 01/31/2014;
$xpire = strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year");
But it is returning 31474800.
Waaaay too complicated. You're doing multiple date<->string conversions, when
php > $x = strtotime('01/31/2014 +1 year');
php > echo date('m/d/Y', $x);
01/31/2015
would do the trick.
Another way is below:
<?php
$date = new DateTime('2014-01-31');
$date->add(new DateInterval('P01Y'));
echo $date->getTimestamp();
?>
There are 2 mistakes here.
You are missing the quote sign " when assigning to $xdate1. It should be
$xdate1 = "01/31/2014";
And the second, to get "01/31/2015", use the date function. strtotime returns a timestamp, not a date format. Therefore, use
$xpire = date("m/d/Y", strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year"));
May I introduce a simple API extension for DateTime with PHP 5.3+:
$xdate1 = Carbon::createFromFormat('m/d/Y', '01/31/2014');
$xpire = $xdate1->addYear(1);
First make $xdate1 as string value like
$xdate1 = '01/31/2014';
then apply date function at it like bellow
$xpire = date('m/d/Y', strtotime($xdate1.' +1 year')); // 01/31/2015

PHP replace a day number with a variable

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.

Categories