Magento : Change date format for an attribute - magento

The date format for my begin_date attribute is displaying like : 2015-04-25 00:00:00
and i want it like : 25 04 2015
I tried :
<?php
echo $this->helper('core')->formatDate(getbegin_date(), Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
?>
But it don't work.

Here is how you can format the time (in PHP). If you need more reference then check this page: Playing with Dates in Magento
$current_time = Mage::getModel('core/date')->timestamp(time());
echo $date = date('d m Y', $current_time);

Related

How to create a list of due dates which are dynamically added base from approved date in Laravel?

I am doing a credit collection app which offer loans to the borrowers. I've been stock in this for so many days now because I can't think of a better way to do it.
SO basically I want to achieve is to have a list of due dates which I will display in the borrower dashboard. for example, the borrower applied for a 2 months loan duration and bi-monthly payment scheme and let say the admin approve it today. So what I want is base from the date today, 12-10-2021 the system will generate 4 due dates(12-25-2021, 12-8-2021, 1-23-2022 and 02-7-2022) and will save them to a separate due dates table.
With regards to the payments, I can analyze the part where the system can check if the payment is done on or before the first due date but how can I tell to the system that the payment is advance payment for the next 3 due date?
what I only have right now is this
$date_approval = Carbon::createFromTimestamp(strtotime($loan_application->date_approval));
$scheme_numdays = $loan_application->scheme->num_days;
$days = (intdiv($date_approval->diff(Carbon::now())->days , $scheme_numdays) + 1) * $scheme_numdays
$due_date = $date_approval->addDays($days)->format('M d Y');
Which can identify the first due date.
Can you help to point to any resources out there than will help me achieve my objective? I am hoping I was able to describe the question and my objective clearly. Any suggestions will be very much appreciated. Thank you so much in advance!
RESPONSE FOR #GRANT
I am following your code
$date_approval = Carbon::createFromTimestamp(strtotime($loanapplication->date_approval));
$scheme_numdays = $loanapplication->scheme->num_days;
$days = (intdiv($date_approval->diff(Carbon::now())->days , $scheme_numdays) + 1) * $scheme_numdays;
$due_date = $date_approval->addDays($days)->format('Y-m-d');
$due_date_plus = $date_approval->addDays(60)->format('Y-m-d');
# Carbon Period usage:
$result = CarbonPeriod::create($due_date, $scheme_numdays.' days', $due_date_plus);
I replace the '# days' in the $result with $scheme_numdays so which contains the number of days.
Then I display it like this,
<div class="col-md-6">
{{$date_approval}}<br/>
{{$loanapplication->date_approval}}<br/>
#foreach ($result as $dt)
{{$dt->format("M d Y")}} <br/>
#endforeach
</div>
the result is
2022-02-27 00:00:00
2021-12-14
Dec 29 2021
Jan 13 2022
Jan 28 2022
Feb 12 2022
Feb 27 2022
Question:
why {{$date_approval}} (2022-02-27 00:00:00) and {{$loanapplication->date_approval}} (2021-12-14) gave different result? The date 2021-12-14 is what I have in my database.
My second question is why the result is five instead of 4 only?
The Data I'm displaying is, the loan duration is 60 days and the payment scheme is on every 15 days or fortnightly.
CarbonPeriod would be perfect for this particular scenario. Have you tried the following:
$date_approval = Carbon::createFromTimestamp(strtotime($loan_application->date_approval));
$scheme_numdays = $loan_application->scheme->num_days;
$days = (intdiv($date_approval->diff(Carbon::now())->days , $scheme_numdays) + 1) * $scheme_numdays
$due_date = $date_approval->addDays($days)->format('Y-m-d');
$due_date_plus = $date_approval->addMonths(8)->format('Y-m-d');
# Carbon Period usage:
$result = CarbonPeriod::create($due_date, '2 months', $due_date_plus);
foreach ($result as $dt) {
echo $dt->format("M d Y");
}
You may need to namespace it:
use Carbon\CarbonPeriod;
Edited answer for followup questions:
<div class="col-md-6">
{{ $date_approval->format('M d Y') }}<br/>
{{ $loanapplication->date_approval->format('M d Y') }}<br/>
#foreach ($result as $dt)
#if($loop->index > 3)
#break
#endif
{{ $dt->format('M d Y') }}<br />
#endforeach
</div>
An alternative is date casting when getting dates from models.
(in loan application model):
protected $casts = [
'date_approval' => 'datetime:M d Y',
];

How to split date and time from a string like 8/29/2011 1:00 PM in laravel

in my blade there is a DateTime picker which selects in mm/dd/yyyy hh:mm AM/PM format. and i need to extract this to date = 8/29/2011 and time = 13:00 .
how can I do that?
Do as below.
$stringdate = "8/29/2011 1:00 PM";
$timestemp = strtotime($stringdate);
$date = date('Y-m-d', $timestemp);
$time = date('H:i:s',$timestemp);
echo $date;
echo $time;
check example
Edit:- as you want this format YYYY-MM-DD hh:mm
$datetime = date('Y-m-d H:i', strtotime("8/29/2011 1:00 PM"));
echo $datetime; //output "2011-08-29 13:00";
Try this way.
#php
$input = '2017-06-27 12:58:20';
$format1 = 'Y-m-d';
$format2 = 'H:i:s';
$date = Carbon\Carbon::parse($input)->format($format1);
$time = Carbon\Carbon::parse($input)->format($format2);
#endphp
It is simple just try it
$date="8/29/2011 1:00 PM"; // date that get from view (blade page)
$createTimestamp = new DateTime($date); // create timestamp object from string
$date = $createTimestamp->format('m/d/Y'); // get date
$time = $createTimestamp->format('H:i:s A'); // get time with AM/PM format
return "date: ".$date." time: ".$time; // return your result
Try this way
Use Carbon;
$dateAndTime=Carbon::parse('8/29/2011 1:00 PM');
$date=$dateAndTime->format('d-m-Y');// 29-8-2011
$time=$dateAndTime->format('H:i:s');// 1:00
$time=$dateAndTime->format('H:i:s A');// 1:00 AM/PM
Refer this link to understand more about Carbon.

How to change the date language in laravel?

I want to change the language of the date i get in my view from english to french
{{ strftime("%d %B %Y, %H:%M", strtotime($article->created_at)) }}
we can use another method with translatedFormat
Carbon::setLocale('fr');
echo $base_weight_category->created_at->translatedFormat('l jS F Y');
we can see result jeudi 2 avril 2020
Set Carbon locale first, then access
Carbon::setLocale('fr');
$date_to_show_in_view = $article->created_at->diffForHumans();
For your second query(to get 15 Mars 2018), use this-
<?php
setlocale(LC_TIME, 'French');
echo $base_weight_category->created_at->formatLocalized('%d %B %Y');
?>

How to use timezones in Laravel?

The user creates date in format like as: 12/23/2016 22:10
After this data should be sent convert to another timezone, for example GMT + 2:
12/23/2016 00:10
I can assume the date that I create should be saved in GTM 0, only after I can change timezone.
How can I use this mechanism in Laravel?
You can use Carbon to change the the timezone of a date as:
$date = Carbon\Carbon::parse('12/23/2016 22:10', 'GMT');
And to add +2 you can do as:
$date->addHours(2)
You can change the timezone for your application in: /config/app.php,
The default timezone is UTC. You can find all supported timezones here: http://php.net/manual/en/timezones.php
Mostly I'm agree with Amit Gupa answer, but the best way to manage timezone conversion isn't by adding hours, but by converting it on Carbon:
$yourdate->timezone('somecountrytimezone')
This way you won't have to worry about different time management between countries (Like daylight saving time) and just manage to get your works done.
Please refer do the documentation : http://carbon.nesbot.com/docs/#api-settersfluent
We can use parse and createFromFormat methods of Carbon to set our datetime as a certain timezone then use setTimezone to convert it to another timezone.
$date = "2022-08-05 12:00:00"; //set datetime for checking
$now = Carbon::now()->timezone('Europe/London')->format('Y-m-d H:i:s');
$createFrom = Carbon::createFromFormat('Y-m-d H:i:s', $date, 'Asia/Hong_Kong')->format('Y-m-d H:i:s');
$parse = Carbon::parse($date,'Asia/Hong_Kong')->setTimezone('Europe/London')->format('Y-m-d H:i:s');
$converted = Carbon::parse($date,'Asia/Hong_Kong')->timezone('Europe/London')->format('Y-m-d H:i:s');
outputs: when dd($now,$createFrom,$parse,$converted);
^ "2022-08-05 08:33:48"
^ "2022-08-05 12:00:00"
^ "2022-08-05 05:00:00"
^ "2022-08-05 05:00:00"
that's not related to laravel , it's just a php , so you can simply get the time-zone offset (exp GMT+3) then do like this :
$timeZoneOffset ---> for GMT+3 you put (3)
function getNewDateByTimeZone($date, $timeZoneOffset)
{
/* if the client Time zone is GMT */
if (!$timeZoneOffset || $timeZoneOffset == 0) {
$timeZoneOffset = "-0";
}
$op = $timeZoneOffset[0];
$timeZoneOffset = preg_replace('/[^0-9.]+/', '', $timeZoneOffset) * 60;
$userDate = date($date, time());
$timeStamp = strtotime($userDate);
$offsetTime = $op == '-' ? $timeStamp + $timeZoneOffset : $timeStamp - $timeZoneOffset;
return date("Y-m-d H:i", $offsetTime);
}
so like that you will save the date in GMT-0

Codeigniter time showing as AM not PM

I am using the codeigniter time helper to echo the TIMESTAMP (CURRENT_TIMESTAMP) row of my mysql database.
The timestamp in my database in raw format is: 2011-11-15 14:40:45
When I echo the time in my view using the helper i get the following: 15/11/2011 02:40
My time now appears to be in AM. Why???
This is the code I use to echo the time:
$the_date = mdate('%d/%m/%Y %h:%i', strtotime($row->date))
echo $the_date
You need to change the format of your data when returning it from the database. Chage the lowercase h (%h) to a capital H (%H) to return 24-hour format rather than the 12 hour you're currently getting.
You're code should look like the below:
$the_date = mdate('%d/%m/%Y %H:%i', strtotime($row->date))

Resources