String date current date/time? - php

I am using $date = date("D M d, Y G:i");.
When I echo $date, it shows the correct date/time. Now I need this as an string.
I have tried string($date); but nothing happens here. And
$today = strtotime($date);
here I get weird numbers..
I need a string so I can put $today in a message.
What is the correct method for this?

The date() function already returns a string.
Doing this :
$date = date("D M d, Y G:i");
You'll have the current date in the $date variable, as a string -- no need for any additional operation.

If you like working with objects you can do this:
$date = new \DateTime('now');
echo $date->format('D M d, Y G:i');

Your $date variable is a string, there's no need for any conversion.
You can have a look at the documentation: http://ch.php.net/manual/en/function.date.php. The return value of the date() function is string.
The strange numbers you see when you call strtotime() is the Unix timestamp which represents the number of seconds elapsed since January 1 1970 00:00:00 UTC.

You're already getting a string. $date can be used like any string now.
strtotime() actually gives you the number of seconds in time like unix

$date = 'Today is '.date("D M d, Y G:i", time());
echo $date;

With regards to:
$today = strtotime($date);
Those numbers are the current timestamp (the number of seconds since January 1st 1970).
You can use this as a second parameter in the date function to change the date to whatever you want.
$newDate = date("D M d, Y G:i", $timeStamp);

Related

How to use date() and strtotime() to get a date from a string?

I am taking input following Datetime : 12.01.2017 2:25 pm
But while I passed above datetime on php function strtotime then it's doesn't returning exact datetime as 24 hours format.
Here is my scripts:
$sdate="12.01.2017 2:25 pm";
date("d.m.Y H:m", strtotime($sdate))
It's returning always 12-jan-2017 14:01:00
But accurate output should be : 12-jan-2017 14:25:00
Please let me know how can get accurate datetime.
Thanks
hey there is problem on your dateformat string :
it should be H:i instead of H:m on your scripts.
Please correct date and format it accordingly your expectation:
date("d-M-Y H:i", strtotime($sdate));
<?php
$sdate="12.01.2017 2:25 pm";
echo date("d-M-Y H:i", strtotime($sdate));
?>
Output
You should use i instead of m in the date:
like this
echo $sdate="12.01.2017 2:25 pm";
echo date("d.m.Y H:i", strtotime($sdate));
There are already answers that use strtotime(). I would like to show you the DateTime class. I find it very usefull and better looking in general. Not that it matters to most people but still.
// Date string
$dateString = '12.01.2017 11:03 am';
// Create date object from string
$oDate = DateTime::createFromFormat('d.m.Y h:i a', $dateString);
// Output the date
echo $oDate->format('d.m.Y h:i a');
// Or some other format
echo $oDate->format('Y-m-d H:i:s');
Note that date() and DateTime accept the same date parameters. Your string suggests that you will need the following parameters.
d - Day of the month, 2 digits with leading zeros
m - Numeric representation of a month, with leading zeros
Y - A full numeric representation of a year, 4 digits
h - 12-hour format of an hour with leading zeros
i - Minutes with leading zeros
a - Lowercase Ante meridiem and Post meridiem
More information
For more information about date, DateTime, strtotime() and all parameters you can use:
date() - Manual
DateTime - Manual
strtotime() - Manual
date() and DateTime manuals list all possible parameters

strtotime() Is Adding An Extra Day

Can anybody tell me why strtotime() seems to be adding 1 day? This seems to only happen in the late afternoon (something like 7 or 8 PM), otherwise it says the correct day.
echo date('m/d/Y h:i:s a', time());
Output:
12/21/2015 08:34:43 pm
echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
Output:
Tuesday, December 22nd, 2015
I would like the above output, however, I want today's date (the 21st not the 22nd).
Use date instead of gmdate.
You are using gmdate() which gets the date in UTC. The problem only happens late in the afternoon/evening because at those times it really is the next day in UTC time.
You're also doing too much work - you can simplify that line of code to this:
// echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
echo date('l, F jS, Y');
Otherwise you've created a timestamp from a time string based on the current time stamp. You could just leave the second parameter to date empty and the current time "now" is assumed.
It is also very important to make sure you are calling date_default_timezone_set somewhere or that you have it configured in your php.ini.
This detail in your code...
echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
(= the "gmdate") will always return Greenwich Mean Time (GMT), which is London/UK.
So change that to date(....
And add date_default_timezone_set('America/New_York'); anyway...
Decided to ultimately use:
$date = new DateTime(date('Y-m-d'), new DateTimeZone('America/New_York'));
$timestamp = $date->format('U');
$date = gmdate('l, F jS, Y', $timestamp);
based on Alexander's comment.

Getting my head around strtotime once and for all

I have a date, in the format yyyy-mm-dd that has been pulled from a MySQL database. This date is saved to a variable $myDate.
I want to save the date, three weeks before this variable's date, to a new variable called $otherDate. How can I do this (and ideally, change the format of the date at the same time)?
I tried:
$otherDate = date("l d F", strtotime("-3 weeks", $myDate));
but to no avail.
The best way to get your head around strtotime is to look at the doc page for it:
The format expected is int strtotime ( string $time [, int $now = time() ] )
And the parameters are defined as:
time
A date/time string. Valid formats are explained in Date and Time Formats.
now
The timestamp which is used as a base for the calculation of relative dates.
Therefore $otherDate = date("l d F", strtotime("-3 weeks", $myDate)); is not correct since $myDate is not an integer but a string in yyyy-mm-dd format.
You must pass the $myDate into the string with the -3 weeks modifier:
$otherDate = date("l d F", strtotime("$myDate -3 weeks"));
Or if you want to redefine $now within the strtotime parameters:
$otherDate = date("l d F", strtotime("-3 weeks", strtotime($myDate)));
I would however recommend using a DateTime object:
$otherDate = new DateTime($myDate);
$otherDate->sub(new DateInterval('P3W'));
echo $otherDate->format('l d F');
The second argument to strtotime() must be a timestamp, not a string, so
$otherDate = date("l d F", strtotime("-3 weeks", strtotime($myDate)));
But look at using DateTime objects instead
$date = new \DateTime( $myDate );
$otherDate = clone $date;
$otherDate->sub( new \DateInterval('P3W') );
echo $otherDate->format( 'l d F' );
Since I find procedural date manipulation ugly and constrictive, I'm going to suggest using PHP's awesome DateTime class.
$dt = new DateTime('2014-11-28');
$dt->modify('-3 weeks');
echo $dt->format('l d F');
Because you are using the strtotime wrong way.
Check this:
$otherDate = date("l d F", strtotime("2014-11-28 -3 weeks"));
echo $otherDate;
Output:
Friday 07 November

PHP date conversion

I have date in format 2011-01-28 06:34:33 i.e. date("Y-m-d H:i:s"). I want to convert it into 28th January 2011.
How can I change it?
Supply the date function with your format, which can be found here. Pass the timestamp of your original date as the second parameter to date. You can obtain the timestamp by using strtotime.
date("dS F Y", strtotime("2011-01-28 06:34:33"));
Use
$dateStr = date("jS F Y", time());
The day value is without leading zero.
Try this echo date('jS F Y h:i:s A');

How to reformat date in PHP?

I am pulling the dates of various posts from a database. The dates are in the following format:
2009-08-12
Numeric Year - Numeric Month - Numeric Day
How can I reformat these dates to something more user friendly like:
August 12, 2009
Numeric Month Numeric Date, Numeric Year
Assuming that the date gotten from the mysql database is stored in a variable called:
$date = $row['date_selected'];
Unlike the strtotime based examples, this allows you to ensure the month and day are interpreted in the correct order regardless of locale settings specified on the server.
$date = DateTime::createFromFormat('Y-m-d', '2009-08-12');
$output = $date->format('F j, Y');
date("F d, Y", strtotime($input))
$new_format = date("Your Date String", strtotime($date));
See:
- http://php.net/strtotime
- http://php.net/date
Basically, if strtotime() can read it correctly, you can reformat it anyway you please.
In this case, Year - Month - Day is a properly recognized strtotime() format, this might not be the case for other formats.
You might consider doing your date formatting in MySQL with your select statement:
DATE_FORMAT(date,'%M %e, %Y') as date_selected
http://www.w3schools.com/sql/func_date_format.asp
<?php
echo date('F j, Y', strtotime($date));
You might want to look at the php function strtotime:
http://php.net/manual/en/function.strtotime.php
It'll parse a large number of date representations to a Unix timestamp.
Then use the date function.
Using strtodate or explode to split the date into its different components, you can then use the date function with the appropriate format string:http://php.net/manual/en/function.date.php
$date = "2009-08-12";
list($year,$month,$day) = explode("-",$date);
$formattedDate = date("F d, Y", mktime(0,0,0,$month,$day,$year));
Outputs: "August 12, 2009"
<?php
//Date Formatter
/*
date: date you want to convert
format: its current format ie m-d-Y, m/d/Y, Y-m-d, Y/m/d
delimS: Current delimiter ie - or / or .
delimF: The delimiter you want for the result
NOTE: this will only convert m-d-Y to Y-m-d and back
*/
function dtform($date,$format,$delimS,$delimF){
$dateFinal = '';
if($format == 'm'.$delimS.'d'.$delimS.'Y'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[2].$delimF.$dateFinal_exp[0].$delimF.$dateFinal_exp[1];
}else if($format == 'Y'.$delimS.'m'.$delimS.'d'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[1].$delimF.$dateFinal_exp[2].$delimF.$dateFinal_exp[0];
}
return $dateFinal;
}
?>
Use it like this:
// February 1, 2005
print date ("F j, Y", mktime (0,0,0,14,1,2004));

Categories