Date Time format method for standard time (laravel 5.3)

12,441

Solution 1

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');

Solution 2

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';
    } 

Solution 3

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.

Solution 4

Format it the way you want.

$carbon->format('Y-m-d H:i:s')
$carbon->format('Y, m, d, H, i, s')
Share:
12,441
Ramzan Mahmood
Author by

Ramzan Mahmood

software Engineer

Updated on June 15, 2022

Comments

  • Ramzan Mahmood
    Ramzan Mahmood almost 2 years

    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

    • ccKep
      ccKep over 7 years
      According to the docs of carbon there's a format function. Eg. $carbon->format("Y, m, d, H, i, s")
    • Ramzan Mahmood
      Ramzan Mahmood over 7 years
      i tried to found but there is nothing to format as above i asked
    • Saumya Rastogi
      Saumya Rastogi over 7 years
      Whats there inside datetime, can you specify the value & its type
    • Ramzan Mahmood
      Ramzan Mahmood over 7 years
      @ccKep can you share link please
    • ccKep
      ccKep over 7 years
    • Ramzan Mahmood
      Ramzan Mahmood over 7 years
      i use this method $carbon->format("Y, m, d, H, i, s") it giving result as 2016-12-27 07:18:37 but i want as (2012, 9, 25, 10, 26, 11)
    • ccKep
      ccKep over 7 years
      I don't think it's clear what you're trying to achieve here.
  • Ramzan Mahmood
    Ramzan Mahmood over 7 years
    its not working when i pass $carbon to setDateTime it gives an error Missing argument 2 for Carbon\Carbon::setDateTime()
  • Rajender Joshi
    Rajender Joshi over 7 years
    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.
  • ccKep
    ccKep over 7 years
    Why would you pass $carbon to setDateTime ? setDateTime is a function of $carbon.
  • Ramzan Mahmood
    Ramzan Mahmood over 7 years
    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(),
  • Ramzan Mahmood
    Ramzan Mahmood over 7 years
    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
  • AddWeb Solution Pvt Ltd
    AddWeb Solution Pvt Ltd over 7 years
    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.