PHP getdate() outputs only year, and not current - php

I have this php code that I want to output the current year, month and day.
date_default_timezone_set('Sweden/Stockholm');
$time_info = getdate();
$day = $time_info['mday'];
$month = $time_info["mon"];
$year = $time_info['year'];
$date = "$year, $month, $day";
And the output:
2014 10 29
But I want it to output
year-month-day
But when I change the $date to $date = $year."-".$month."-".$day."-"; the output is: 1975.
Obviously, this is wrong, so how can I fix it. And a explanation why this occurs would also be great.
EDIT:
Ok, so according to #Marc B and #Barry , it did at some point math, and they were right. I don't know why this occurs, but I got it sorted out. Thanks!

Why don't you use the php date function?
echo date("Y-m-d");

You can get that if you run this.
$time_info = getdate();
echo $time_info->format('Y-m-d');
Using PHP format() function resolves your problem.
Documentation is here.

use php date function Use Like this
date_default_timezone_set('Sweden/Stockholm');
echo date("Y-m-d");

Related

Get y-m from y-m-d in date PHP

I am trying to get only the month and year on this format (y-m) coming from y-m-d format of date.
I am getting an error saying Call to a member function format() on a non-object.
Am i doing it correctly? Thank you for your help with this.
$current_date= '2017-04-03';
$d = DateTime::createFromFormat("Y-m",$current_date);
$current_date = $d->format("Y-m");
echo $current_date;
My apologies, my bad, the reason for the error was I put the following code inside the for loop which causes error.
After moving the code outside the loop, the only thing I changed was the code from
$d = DateTime::createFromFormat("Y-m",$current_date);
to
$d = DateTime::createFromFormat("Y-m-d",$current_date);
Thanks alot!
Corrected code:
$current_date= '2017-04-03';
$d = DateTime::createFromFormat("Y-m-d",$current_date);
$current_date = $d->format("Y-m");
echo $current_date;

Carbon::now() - only month

I couldn't find anywhere in documentation how to show current year or month with Carbon?
when i write this:
Carbon\Carbon::now('m');
it gives me the whole time stamp, but I just need the month
like
date('m');
but it must be Carbon!
How can I achieve this?
$now = Carbon::now();
echo $now->year;
echo $now->month;
echo $now->weekOfYear;
Update:
even this works since Laravel 5.5^
echo now()->month
I think you've already worked this out in a comment, but just for clarity: Carbon extends PHP's native DateTime class, so you can use any of the methods available on that, like format:
Carbon::now()->format('M');
(where M is the modifier for A short textual representation of a month, three letters)
You can use these both ways to get the current month
Carbon::now()->month;
or
Carbon::now()->format('m');
Just use this in your any blade file for print year:
{{ \Carbon\Carbon::now()->year }}
I wanted to get the current month and got to this question, to get the current month:
$now = Carbon::now();
$monthStart = $now->startOfMonth();
w/ Carbon + Laravel:
now()->format('M')
use Carbon\Carbon;
$today= Carbon::now();
echo $today->year;
echo $today->month;
echo $today->day;
echo $today->hour;
echo $today->minute;
echo $today->second;
Laravel 8
return date('m');
or
return now()->format('m');
You cant call it statically
use
$now = Carbon::now();
echo $now->month

Format mySQL timestamp by changing date-month and remove seconds using PHP function?

I echo $istoriko['timestamp'] from mySQL and I get 2014-04-24 00:10:29
How can I change it to
24/04/14 (or 2014) 00:10
Thank you
Try this, its simple
You can set any date format now in date functions
echo date("d/m/y",strtotime($istoriko['timestamp']));
Follow functions can help you:
$date = '2014-04-24 00:10:29';
$date = date('d/m/Y H:i',strtotime($date));
echo $date

how to get the next year date from the date value stored in a php variable

I am trying to get the next year date from a php variable which holds the date value posted from a html form.
Below is the code
$warranty_from = mysql_real_escape_string($_POST['from_date']);
Say the $warranty_from holds the value 2013-06-04. I want to get the next year date i.e
2014-06-04 and store into $warranty_to variable.
Searched lot over the net but couldnt find any resource related to my issue. Any help is appreciated. Thanks in advance.
The following should work:
$warranty_from = '2013-06-04';
$warranty_to = date('y-m-d', strtotime('+1 years', strtotime($warranty_from)));
Something like that should work
This was exact code
$newWarranty_date = date("Y-m-d",strtotime("+1 year",strtotime($warranty_from)));
Try this one
<?php
$warranty_to = date('Y-m-d', strtotime('+1 year', strtotime($warranty_from)));
?>
Calculations like this are trivial using the DateTime classes:-
$_POST['from_date'] = '2013-06-04';//Just for this example.
$warranty = DateTime::createFromFormat('Y-m-d', $_POST['from_date']);
$interval = new DateInterval('P1Y');
$warranty->add($interval);
echo $warranty->format('Y-m-d');
Will output:-
2014-06-04

PHP - Turn a timestamp (2012-07-12 20:26:00) Into two seperate variables that refelct the month and day values?

I would like to separately strip the day and month values out of this timestamp "2012-07-12 17:50:00".
So essentially I could have two variables like:
$month = 7
$day = 12
As I am not very proficient with php, specifically regex coding, I thought I would post this question to ask for advice as to how I would go about accomplishing something like this.
Any assistance, insight or input in this regard would be greatly appreciated. Thank you!
AS A NOTE: Futhermore, I would like to turn the two variables into an output like "12th July". This can be coded quite easily so I have not made this a part of the question but if there is a simple function that deals with this information would be much appreciated too!
As said in the comments, DateTime would be a good choice.
First, if your php config dosen't already set it up for you, set your default timezone by:
date_default_timezone_set('XXXX');
XXXX stand for a value out of the List of supported timezones
initialize your date by:
$DateTime = new DateTime();
depending on where you get the timestamp, lets assume you will create it out of PHP:
$timestamp = $DateTime->getTimestamp();
Now format the output
echo $month = $DateTime->format( 'm' );
echo $day = $DateTime->format( 'd' );
or to get you desired output:
echo $output = $DateTime->format( 'dS F' );
<?php
list($month,$day) = explode(':',date('n:j',strtotime('2012-07-12 17:50:00')),2);
echo 'Month: '.$month.'<br />'."\n";//Month: 7
echo 'Day: '.$day.'<br />'."\n";//Day: 12
?>
And...
<?php
echo date('jS F',strtotime('2012-07-12 17:50:00'));//Outputs: 12th July
?>
Assuming the timestamp is a string, something like this should work if you're looking for a regex solution...
<?php
$string = '2012-07-12 17:50:00';
preg_match_all('/\d{4}-(\d{2})-(\d{2})/', $string, $matches);
$month =$matches[1][0];
$day = $matches[2][0];
?>

Categories