Date Time format method for standard time (laravel 5.3)

Its my method to get created date from DB to show in a proper format

function showDateTime($student_id, $datetime)
   {

    $time_zone_app = DB::table('students')->where('id', $student_id_id)->value('time_zone');
    $zone_data = explode(':', $time_zone_app);

    $time_zone['hours']   = $zone_data[0];
    $time_zone['minutes'] = $zone_data[1];

    $carbon = new Carbon\Carbon();
    $carbon->setDateTime(2012, 9, 25, 10, 26, 11);
    $carbon->addHours($time_zone_data[0]);
    $carbon->addHours($time_zone_data[1]);

    $datetime = $carbon->toFormattedDateString();
    return $datetime;

}

Above I have hard code time because i am not getting time as i want if i echo $carbon below $carbon = new Carbon\Carbon(); It gives me time as

2016-9-25 07:04:02

I want to convert this time to format 2016, 9, 25, 7, 4, 02 as

(year, month, day, hours, minutes, seconds) and then want to pass it to setDateTime method above

Please help how can i do

Format it the way you want.

$carbon->format('Y-m-d H:i:s')
$carbon->format('Y, m, d, H, i, s')

Date Time format method for standard time (laravel 5.3 , Its my method to get created date from DB to show in a proper format function showDateTime($student_id, $datetime) { $time_zone_app� Laravel 5.x laravel 5.3 Laravel 5.4 Laravle 5.2 date_format date after_or_equal:date before:date before_or_equal:date after:date Validate Date Format In this post, I would like to show you date format validation in Laravel application, You should always validate forms to ensure that you receive data in the good format.

You can convert carban date formate like:

$datetime = '2016-9-25 07:04:02';    
$newdate = Carbon::parse($datetime)->format('Y, m, d, H, i, s');

This will produce output like:

//output
2016, 09, 25, 07, 04, 02

You can test this thing from here.

DateTime - Manual, An ISO8601 format string for PHP's date functions that's compatible with If you have timezone information in the time string you construct the DateTime object with, closed as "not a bug" even though it's a clear violation of the ISO8601 standard. If you need DateTime::createFromFormat functionality in versions < 5.3.0� ISO 8601 Data elements and interchange formats – Information interchange – Representation of dates and times is an international standard covering the exchange of date- and time-related data. It was issued by the International Organization for Standardization (ISO) and was first published in 1988.

First Option:
If you want to use laravel format then you can flow it
 {{ Carbon\Carbon::parse($quotes->created_at)->format('d-m-Y i') }}

See this url https://laracasts.com/discuss/channels/laravel/how-to-format-a-carbon-date-inside-blade

Second Option: 
if you want to custom format then you can use it.
Use example :
 echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('@1367367755'); # timestamp input
echo time_elapsed_string('2013-05-01 00:22:35', true);
Input can be any supported date and time format.

Output : 4 months ago 4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago Function :

 function time_elapsed_string($datetime, $full = false) {
        $now = new DateTime;
        $ago = new DateTime($datetime);
        $diff = $now->diff($ago);

        $diff->w = floor($diff->d / 7);
        $diff->d -= $diff->w * 7;

        $string = array(
            'y' => 'year',
            'm' => 'month',
            'w' => 'week',
            'd' => 'day',
            'h' => 'hour',
            'i' => 'minute',
            's' => 'second',
        );
        foreach ($string as $k => &$v) {
            if ($diff->$k) {
                $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
            } else {
                unset($string[$k]);
            }
        }    
        if (!$full) $string = array_slice($string, 0, 1);
        return $string ? implode(', ', $string) . ' ago' : 'just now';
    } 

strtotime - Manual, The function expects to be given a string containing an English date format and will try Each parameter of this function uses the default time zone unless a time zone is 5.3.0, Prior to PHP 5.3.0, relative time formats supplied to the datetime � I need to show the date extracted from a database from a model in a controller and sent to the view. The format that is given to me is the following: "2018-02-02 00: 00: 00.000". And I need it to look like this: "02/02/2018". I do not want to use hard code in the view. Total Thanks!

You can also try this:

use Carbon\Carbon;


$now = Carbon::now();

$now->format('Y, m, d, H, i, s');

You can also try following formats.

$now->format('d-m-y H:i:s');

$now->format('d.m.y H:i:s');

Carbon, Carbon - A simple PHP API extension for DateTime. Method added, 1.0.0, $ format, $time, $object = null Returns the offset hour and minute formatted with +/- and a given separator (":" by default). 1.x is compatible with PHP 5.3+. Carbon 2 is officially supported by Laravel since the version 5.8, if you want to use it on� As a result, although the "s" standard format specifier represents a date and time value in a consistent format, the formatting operation does not modify the value of the date and time object that is being formatted to reflect its DateTime.Kind property or its DateTimeOffset.Offset value. For example, the result strings produced by formatting

Eloquent: Mutators - Laravel, Custom Casts; Array & JSON Casting; Date Casting; Query Time Casting. Introduction. Accessors and mutators allow you to format Eloquent attribute values when To define an accessor, create a getFooAttribute method on your model where By default, Eloquent will convert the created_at and updated_at columns to� Showing Date in Form/Views. Ok, we saved the date in Y-m-d format in the database. But for users, visually, it always should be m/d/Y.So, similar to the example above, we need to create accessor on the Model.

Logging - Laravel, By default, Laravel will use the stack channel when logging messages. So, imagine we log a message using the debug method: This contextual data will be formatted and displayed with the log message: ($logger->getHandlers() as $handler) { $handler->setFormatter(new LineFormatter( '[%datetime%] %channel %. Laravel whereBetween method are useful when you retrieve records in between two given dates from timestamp column.. Suppose you want to filter records in database table by from data and to date then you can use whereBetween method to filter the records.

Eloquent: Getting Started - Laravel, Eloquent Model Conventions; Default Attribute Values Inserts; Updates; Mass Assignment; Other Creation Methods This property determines how date attributes are stored in the database, as well as their format when the model is on typical Laravel collections while only loading a single model into memory at a time: You can use this function to get current date time in Laravel. Read more to find laravel tutorial for beginners step by step guidance only at tutorialsplane

Comments
  • According to the docs of carbon there's a format function. Eg. $carbon->format("Y, m, d, H, i, s")
  • i tried to found but there is nothing to format as above i asked
  • Whats there inside datetime, can you specify the value & its type
  • @ccKep can you share link please
  • Carbon Docs on Formatting
  • its not working when i pass $carbon to setDateTime it gives an error Missing argument 2 for Carbon\Carbon::setDateTime()
  • setDateTime works fine for me which I copied from your question. Did you tried updating your carbon library? Or maybe you are missing something somewhere else.
  • Why would you pass $carbon to setDateTime ? setDateTime is a function of $carbon.
  • I try to do it as $newdate = Carbon::parse($datetime)->format('Y, m, d, H, i, s'); $carbon->setDateTime($newdate); its giving error Missing argument 2 for Carbon\Carbon::setDateTime(),
  • its close to solution now problem i am facing is that how to pass this time which i am getting in this format as format('Y, m, d, H, i, s'); to setDateTime method .yon can review it above from my question
  • You can try list($year, $month, $date, $hrs, $min, $sec) = explode(', ', $newdate); to extract all values and use that. I'm not sure how it worth, try it with own risk.