$date =$row2['DeliveryDate'];
$date now contains the date variable as a datetime, to display it I would use:
echo date_format($date, 'm-d-y');
the problem I'm having is extracting single values from $date for example:
$datetime = strtotime($row2['DeliveryDate']);
$mysqldate = date("d", $datetime);
returns this error:
Warning: strtotime() expects parameter 1 to be string, object given in
C:\xampp\htdocs\tutorials\DerBlatt\hebrewDateTrial.php on line 10
I've tried numerous ways to extract the day/month/year into single variables but nothing works
if someone can suggest a way it might work I'll be very greatfull;
I've copy/pasted code from many sites but all of them use an example of the date as a string, unfortunately i haven't found a solution for the datetime variable.
I wanna do something like:
$date =$row2['DeliveryDate'];
//whatever conversion code that comes in between.
$d = //the day from datetime
$m = //the month from datetime
$y = //the year from datetime
If you are using PHP 5.3 or better ,use the DateTime class .
if you want to display in this format $format='m-d-y';
Retrieving data from database .
$date =$row2['DeliveryDate'];
$date = DateTime::createFromFormat('Y-m-d H:i:s',$date);
if($date){ // if the date is correct
$yourdate = $date->format($format);
$year = $date->format('Y');
$month = $date->format('m');
$day = $date->format('d');
}
Saving to database .
$date = DateTime::createFromFormat($format,$date);
if($date){
$date = $date->format('Y-m-d H:i:s');
$row2['DeliveryDate'] = $date;
}else{
$row2['DeliveryDate'] = date('Y-m-d H:i:s');
}
Try this:
echo date('d',strtotime($row2['DeliveryDate']));
i think it will work.
so this is how i made it work (very funny)...
$m = date_format($row2['DeliveryDate'], 'm');
$d = date_format($row2['DeliveryDate'], 'd');
$y = date_format($row2['DeliveryDate'], 'y');
echo 'Month: '.$m.' Day: '.$d.' Year: '.$y;
Thank you guys for helping me, sometimes my best solution is just using my head...
Related
I have the following code:
$date1 = (string)$_POST['convocatory_open_start_date']; // "30/04/2015"
$date2 = (string)$_POST['convocatory_open_end_date']; // "31/05/2015"
$startDate = date('Y-m-d', strtotime($date1));
$endDate = date('Y-m-d', strtotime($date2));
but I always get $startDate and $endDate 1970-01-01 Why???
Please Help I have like 3 hours in the same problem.
Thanks
The problem is that if you use / as a separator, strtotime will assume the format is m/d/Y. So it will not be able to convert it, and it defaults to 1970-01-01.
The easiest solution would be
$startDate = DateTime::createFromFormat('d/m/Y', $date1)->format('Y-m-d');
$endDate = DateTime::createFromFormat('d/m/Y', $date2)->format('Y-m-d');
First check if the posted variables :-
$date1 and $date12 are not NULL
Because NULL is interpreted as 0 by the php function strtotime(), since you are supposed to pass an integer timestamp. A timestamp of 0 means 1-1-1970.
You need to check the posted contents of the posted variables
if(($date1 === NULL) || ($date1 === NULL) ) {
//don't use `strtotime()`
}
If that is not the case and Your variables are posted, the only thing i can think of is that you have problem with the stringTimeFormatting when using the date function and you input format.
$date1 = (string)$_POST['convocatory_open_start_date']; // "30/04/2015"
$date2 = (string)$_POST['convocatory_open_end_date']; // "31/05/2015"
$startDate = date($date1);
$endDate = date($date2);
echo $startDate, '<br/> ' , $endDate;
Hope that adds some to your Quest!
This is my current code:
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year");
$arrivalTime = date('Y-M-d H:i:s', strtotime("+$flightTimeMin minutes", $dateGame));
This isn't working because I believe "$dateGame" is an object. How would I turn it into something readable by "strtotime"?
Thanks
You dont need to use strtotime() since you are using datetime and you can use datetime object to format the date as
$universeTime = 3 ;
$flightTimeMin = 20;
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year +$flightTimeMin minutes");
If you want to format the display you can use as
echo $dateGame->format('Y-m-d H:i:s');
or just use
echo $dateGame->date ;
<?php
$date = "04-15-2013";
$date = strtotime($date);
$date = strtotime("+1 day", $date);
echo date('m-d-Y', $date);
?>
This is driving me crazy and seems so simple. I'm pretty new to PHP, but I can't figure this out. The echo returns 01-01-1970.
The $date will be coming from a POST in the format m-d-Y, I need to add one day and have it as a new variable to be used later.
Do I have to convert $date to Y-m-d, add 1 day, then convert back to m-d-Y?
Would I be better off learning how to use DateTime?
there you go
$date = "04-15-2013";
$date1 = str_replace('-', '/', $date);
$tomorrow = date('m-d-Y',strtotime($date1 . "+1 days"));
echo $tomorrow;
this will output
04-16-2013
Documentation for both function
date
strtotime
$date = DateTime::createFromFormat('m-d-Y', '04-15-2013');
$date->modify('+1 day');
echo $date->format('m-d-Y');
See it in action
Or in PHP 5.4+
echo (DateTime::createFromFormat('m-d-Y', '04-15-2013'))->modify('+1 day')->format('m-d-Y');
reference
DateTime::createFromFormat()
$date = strtotime("+1 day");
echo date('m-d-y',$date);
use http://www.php.net/manual/en/datetime.add.php like
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('1 days'));
echo date_format($date, 'Y-m-d');
output
2000-01-2
The format you've used is not recognized by strtotime(). Replace
$date = "04-15-2013";
by
$date = "04/15/2013";
Or if you want to use - then use the following line with the year in front:
$date = "2013-04-15";
Actually I wanted same alike thing,
To get one year backward date, for a given date! :-)
With the hint of above answer from #mohammad mohsenipur
I got to the following link, via his given link!
Luckily, there is a method same as date_add method, named date_sub method! :-)
I do the following to get done what I wanted!
$date = date_create('2000-01-01');
date_sub($date, date_interval_create_from_date_string('1 years'));
echo date_format($date, 'Y-m-d');
Hopes this answer will help somebody too! :-)
Good luck guys!
In my database I have a time stamp column...which reflects a format like this: 2012-04-02 02:57:54
However I would like to separate them up into $date and $time.
After some research through the php manual...I found that date(), date_format() and strtotime() are able to help me to separate them...(not sure if I am right)
But I am not very sure of how to code it out...
In my php file...the timestamp extracted would be $row['DATETIMEAPP'].
Will
$date = strtotime('d-m-Y',$row['DATETIMEAPP']);
$time = strtotime('Gi.s',$row['DATETIMEAPP']);
or
$date = date('d-m-Y',$row['DATETIMEAPP']);
work?
Can I use date() to get the time as well??
Thanks in advance
$timestamp = strtotime($row['DATETIMEAPP']);
gives you timestamp, which then you can use date to format:
$date = date('d-m-Y', $timestamp);
$time = date('Gi.s', $timestamp);
Alternatively
list($date, $time) = explode('|', date('d-m-Y|Gi.s', $timestamp));
If you dont want to change the format of date and time from the timestamp, you can use the explode function in php
$timestamp = "2012-04-02 02:57:54"
$datetime = explode(" ",$timestamp);
$date = $datetime[0];
$time = $datetime[1];
$mydatetime = "2012-04-02 02:57:54";
$datetimearray = explode(" ", $mydatetime);
$date = $datetimearray[0];
$time = $datetimearray[1];
$reformatted_date = date('d-m-Y',strtotime($date));
$reformatted_time = date('Gi.s',strtotime($time));
You can try this:
For Date:
$date = new DateTime($from_date);
$date = $date->format('d-m-Y');
For Time:
$time = new DateTime($from_date);
$time = $time->format('H:i:s');
$timestamp='2014-11-21 16:38:00';
list($date,$time)=explode(' ',$timestamp);
// just time
preg_match("/ (\d\d:\d\d):\d\d$/",$timestamp,$match);
echo "\n<br>".$match[1];
Works for me:
select DATE( FROM_UNIXTIME( columnname ) ) from tablename;
If you want to use the DateTime class, you can do so like this:
$timestamp = $row['DATETIMEAPP']; // String formatted as "2012-04-02 02:57:54"
// Create DateTime object from custom timestamp
$dt = DateTime::createFromFormat('Y-m-d H:i:s', $timestamp);
$date = $dt->format('d-m-Y'); // String variable of just the date
$time = $dt->format('H:i:s'); // String variable of just the time
And if you're concerned about using DateTime over strtotime() or date(), I'd like to point you in the direction of this conversation on StackOverflow titled "DateTime class vs. native PHP date-functions."
Optionally you can use database function for date/time formatting. For example in MySQL query use:
SELECT DATE_FORMAT(DATETIMEAPP,'%d-%m-%Y') AS date, DATE_FORMT(DATETIMEAPP,'%H:%i:%s') AS time FROM yourtable
I think that over databases provides solutions for date formatting too
I've got a date in this format:
2009-01-01
How do I return the same date but 1 year earlier?
You can use strtotime:
$date = strtotime('2010-01-01 -1 year');
The strtotime function returns a unix timestamp, to get a formatted string you can use date:
echo date('Y-m-d', $date); // echoes '2009-01-01'
Use strtotime() function:
$time = strtotime("-1 year", time());
$date = date("Y-m-d", $time);
Using the DateTime object...
$time = new DateTime('2099-01-01');
$newtime = $time->modify('-1 year')->format('Y-m-d');
Or using now for today
$time = new DateTime('now');
$newtime = $time->modify('-1 year')->format('Y-m-d');
an easiest way which i used and worked well
date('Y-m-d', strtotime('-1 year'));
this worked perfect.. hope this will help someone else too.. :)
On my website, to check if registering people is 18 years old, I simply used the following :
$legalAge = date('Y-m-d', strtotime('-18 year'));
After, only compare the the two dates.
Hope it could help someone.
// set your date here
$mydate = "2009-01-01";
/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastyear = strtotime("-1 year", strtotime($mydate));
// format and display the computed date
echo date("Y-m-d", $lastyear);
Although there are many acceptable answers in response to this question, I don't see any examples of the sub method using the \Datetime object: https://www.php.net/manual/en/datetime.sub.php
So, for reference, you can also use a \DateInterval to modify a \Datetime object:
$date = new \DateTime('2009-01-01');
$date->sub(new \DateInterval('P1Y'));
echo $date->format('Y-m-d');
Which returns:
2008-01-01
For more information about \DateInterval, refer to the documentation: https://www.php.net/manual/en/class.dateinterval.php
You can use the following function to subtract 1 or any years from a date.
function yearstodate($years) {
$now = date("Y-m-d");
$now = explode('-', $now);
$year = $now[0];
$month = $now[1];
$day = $now[2];
$converted_year = $year - $years;
echo $now = $converted_year."-".$month."-".$day;
}
$number_to_subtract = "1";
echo yearstodate($number_to_subtract);
And looking at above examples you can also use the following
$user_age_min = "-"."1";
echo date('Y-m-d', strtotime($user_age_min.'year'));